1

i have problem with decode utf-8 , i try everything on the web and it's still not working ,

i read line from file , this line contains utf8 characters that are not appears

if i set this line manually the characters appears , what are the differences and how can i solve this ?

this code is working :

b="test Test\hello\\\xd7\x92\xd7\x99\xd7\x95\xd7\x9c \xd7\x9b\xd7\x9e\xd7\x95\xd7\xaa\xd7\x99.csv"
print(type(b))
print (b)

the resulat : test Test\hello\גיול כמותי.csv

this code is not working :

f = open('C:\Program Files (x86)\Syslogd\Logs\SyslogCatchAll.txt')
for line in f :
print(line)

the resault is :

bro_files Test\hello\\xd7\x92\xd7\x99\xd7\x95\xd7\x9c \xd7\x9b\xd7\x9e\xd7\x95\xd7\xaa\xd7\x99.csv

the 2 lines are the same , the differences is that i read it from file

newton
  • 11
  • 1

1 Answers1

0

For opening a file with utf-8 you need to use codecs as:

import codecs
f = codecs.open('C:\Program Files (x86)\Syslogd\Logs\SyslogCatchAll.txt', encoding="utf-8")

Or, you may try with io module, as suggested by @ PM 2Ring

import io
io.open("C:\Program Files (x86)\Syslogd\Logs\SyslogCatchAll.txt", encoding="utf8")
ZdaR
  • 22,343
  • 7
  • 66
  • 87