Python: Unicodedecodeerror: 'ascii' Codec Can't Decode Byte 0xef In Position 0: Ordinal Not In Range(128)
I'm currently have an issue with my python 3 code. replace_line('Products.txt', line, tenminus_str) Is the line I'm trying to turn into utf-8, however when I try to do this like
Solution 1:
Change your function to:
defreplace_line(file_name, line_num, text):
withopen(file_name, 'r', encoding='utf8') as f:
lines = f.readlines()
lines[line_num] = text
withopen(file_name, 'w', encoding='utf8') as out:
out.writelines(lines)
encoding='utf8'
will decode your UTF-8 file correctly.
with
automatically closes the file when its block is exited.
Since your file started with \xef
it likely has a UTF-8-encoding byte order mark (BOM) character at the beginning. The above code will maintain that on output, but if you don't want it use utf-8-sig
for the input encoding. Then it will be automatically removed.
Solution 2:
codecs
module is just what you need. detail here
import codecs
def replace_line(file_name, line_num, text):
f = codecs.open(file_name, 'r', encoding='utf-8')
lines = f.readlines()
lines[line_num] = text
f.close()
w = codecs.open(file_name, 'w', encoding='utf-8')
w.writelines(lines)
w.close()
Solution 3:
Handling coding problems You can try adding the following settings to your head
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
Type = sys.getfilesystemencoding()
Solution 4:
Try adding encoding='utf8' if you are reading a file
withopen("../file_path", encoding='utf8'):
# your code
Post a Comment for "Python: Unicodedecodeerror: 'ascii' Codec Can't Decode Byte 0xef In Position 0: Ordinal Not In Range(128)"