To solve UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xef in position error follow below methods.
Traceback (most recent call last): File "<stdin>", line 1, in <module> UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-5: ordinal not in range(128)
How to solve UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xef in position 1 ?
Refer to the given methods to solve the issue.
Set the system default encoding to utf-8 at the beginning of the script so that all strings are encoded in that format.
import sys reload(sys) sys.setdefaultencoding('utf-8')
Method 2 :
You can use the decode() function to solve the issue. Use the given string to decode with ‘utf-8’.
You correctly decoded the string. The issue is with the print command since it converts the Unicode string to console encoding, which the console cannot show. Try placing the string into a file and inspecting the output with a quality Unicode-supporting editor:
import codecs f = codecs.open('out_file.txt', 'w', encoding='utf-8') f.write(string) f.close()
Hope the above solution works.