Skip to content Skip to sidebar Skip to footer

Prevent Encoding Errors In Python

I have scripts which print out messages by the logging system or sometimes print commands. On the Windows console I get error messages like Traceback (most recent call last): Fil

Solution 1:

The problem is that your terminal/shell (cmd as your are on Windows) cannot print every Unicode character.

You can fail-safe encode your strings with the errors argument of the str.encode method. For example you can replace not supported chars with ? by setting errors='replace'.

>>>s = u'\u2019'>>>print s
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\encodings\cp850.py", line 12, in encode
    return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can\'t encode character u'\u2019' in position
 0: character maps to <undefined>
>>>print s.encode('cp850', errors='replace')
?

See the documentation for other options.

Edit If you want a general solution for the logging, you can subclass StreamHandler:

classCustomStreamHandler(logging.StreamHandler):defemit(self, record):
        record = record.encode('cp850', errors='replace')
        logging.StreamHandler.emit(self, record)

Post a Comment for "Prevent Encoding Errors In Python"