Search code examples
pythonwxpython

Printing out text file in python gives error message - charmap_decode(input,errors,decoding_table)


I am printing out the contents of a text file in python using wxpython in a gui window, but get the error message:

return _core_.TextEntryBase_WriteText(*args, **kwargs)
  File "C:\Python27\lib\encodings\cp1252.py", line 15, in decode
    return codecs.charmap_decode(input,errors,decoding_table)
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 60: character maps to <undefined>

This is the line in the text file which trips it up:

         <block count="55770" name="BASIC_LATIN">
 !"%'(),-.0123456789:?ABCDEFGHIJKLMNOPQRSTUVWYZabcdefghijklmnopqrstuvwxyz</block>
         <block count="2" name="LATIN_1_SUPPLEMENT">éï</block>

Any ideas on how I can get round this issue please?


Solution

  • There is a ton of information about wxPython and unicode on their wiki page:

    I always use the unicode build of wxPython. I think the newer versions of wx may only be unicode versions. Anyway as joaquin pointed out, you also need to open the file using the right encoding. Python cannot guess which version of unicode a file is encoded in. If you don't care about keeping the unicode, there is a handy module called Unidecode that I've found useful for converting unicode to ASCII.