Decoding Error: Character Mapped to Undefined
While attempting to read a text file, a Python 3 program encounters an error:
UnicodeDecodeError: 'charmap' codec can't decode byte 0x90 in position 2907500: character maps to `<undefined>`
Explanation:
This error signifies that the 'charmap' codec cannot decode a specific byte in the text file. The 'charmap' codec is used for character encodings that map bytes to characters directly.
Solution:
To resolve this error, it is necessary to identify the correct encoding of the text file and specify it when opening the file. Since the byte 0x90 does not have a valid mapping in the CP1252 encoding, the file likely uses a different encoding, such as UTF-8 or Latin-1.
To open the file using the correct encoding, add the encoding argument:
file = open(filename, encoding="utf-8")
For Latin-1 encoding:
file = open(filename, encoding="latin-1")
Note:
Refer to the documentation on "How to determine the encoding of text" to ascertain the correct encoding of the text file.
The above is the detailed content of How to Fix Python's 'UnicodeDecodeError: 'charmap' codec can't decode byte...'?. For more information, please follow other related articles on the PHP Chinese website!