32
votes

I keep getting this error while reading a text file. Is it possible to handle/ignore it and proceed?

UnicodeEncodeError: ‘charmap’ codec can’t decode byte 0x81 in position 7827: character maps to undefined.

3
This varies a lot based on details. Python 2? Python 3? Are you trying to decode strings you already read? How? Etc. - Charles Duffy
Python 3. I'm using readlines() - Bob
Okay -- updated the question to specify Python 3. Unicode is one of the places where there are very big differences between 2 and 3; please be sure to specify version explicitly in the future. - Charles Duffy
For a more general case, it is probably worth looking at this: stackoverflow.com/questions/58199571/… - Dragonthoughts

3 Answers

62
votes

In Python 3, pass an appropriate errors= value (such as errors=ignore or errors=replace) on creating your file object (presuming it to be a subclass of io.TextIOWrapper -- and if it isn't, consider wrapping it in one!); also, consider passing a more likely encoding than charmap (when you aren't sure, utf-8 is always a good place to start).

For instance:

f = open('misc-notes.txt', encoding='utf-8', errors='ignore')

In Python 2, the read() operation simply returns bytes; the trick, then, is decoding them to get them into a string (if you do, in fact, want characters as opposed to bytes). If you don't have a better guess for their real encoding:

your_string.decode('utf-8', 'replace')

...to replace unhandled characters, or

your_string.decode('utf-8', 'ignore')

to simply ignore them.

That said, finding and using their real encoding (rather than guessing utf-8) would be preferred.

1
votes

You should open the file with a codecs to make sure that the file gets interpreted as UTF8.

import codecs
fd = codecs.open(filename,'r',encoding='utf-8')
data = fd.read()
-4
votes

Yeah..you could wrap it in a

try:
    ....
except UnicodeEncodeError: 
    pass