. Advertisement .
..3..
. Advertisement .
..4..
Hi everyone, I’m learning about python. While working, I try to read my text file in Python3. As a result, I get the message:
Traceback (most recent call last):
File "SCRIPT LOCATION", line NUMBER, in <module>
text = file.read()`
File "C:\Python31\lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x90 in position 2907500: character maps to `<undefined>`
What can I do about the “unicodedecodeerror: ‘charmap’ codec can’t decode byte” issue? Is there a better approach?
The cause: The reason is that the file in question does not employ CP1252 encoding. It makes use of a different encoding. Latin-1 and UTF-8 are popular ones. Latin-1 is less likely since 0x90 is a continuation byte in UTF-8, where it is more likely.
Solution:
When opening the file, you must provide the encoding to avoid the “unicodedecodeerror: ‘charmap’ codec can’t decode byte” issue:
file = open(filename, encoding="utf-8")
is not working? Tryfile = open(filename, errors="ignore")
if you need to remove unnecessary characters. (docs)