UnicodeEncodeError when not running script from IDE

Dave Angel davea at davea.name
Tue Feb 12 10:58:21 EST 2013


On 02/12/2013 10:29 AM, Magnus Pettersson wrote:
>> Are you sure you are writing the same data? That would mean that pydev
>>
>> changes the default encoding -- which is evil.
>>
>>
>>
>> A portable approach would be to use codecs.open() or io.open() instead of
>>
>> the built-in:
>>
>>
>>
>> import io
>>
>> with io.open(filepath, "a") as f:
>>
>>      ...
>>
>>
>>
>> io.open() uses UTF-8 by default, but you can specify other encodings with

I think you are using Python 2.x, not Python 3.  So you'd better be 
explicit what encodings you want for each file.

>>
>> io.open(filepath, mode, encoding=whatever).
>
>
> Interesting. Pydev must be doing something behind the scenes because when i changed open() to io.open() i get error inside of eclipse now:

What encoding is this file?  Since you're appending to it, you really 
need to match the pre-existing encoding, or the next program to deal 
with it is in big trouble.  So using the io.open() without the encoding= 
keyword is probably a mistake.

>
> f.write(card+"\n")
>    File "C:\python27\lib\encodings\cp1252.py", line 19, in encode
>      return codecs.charmap_encode(input,self.errors,encoding_table)[0]
> UnicodeEncodeError: 'charmap' codec can't encode character u'\u53c8' in position 32: character maps to <undefined>
>
> ....
>


-- 
DaveA



More information about the Python-list mailing list