1

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?

3
  • see also: stackoverflow.com/questions/3284827/… Commented Feb 13, 2014 at 13:48
  • How do I use utf-8 in lines = infile.readlines() Commented Feb 13, 2014 at 13:56
  • you should open() in the adequate mode, like infile = open('test.txt', encoding='latin-1'). If infile in your comment is a file object you already opened the file. Commented Feb 13, 2014 at 14:01

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.