Encoding problem when launching Python27 via DOS

MRAB python at mrabarnett.plus.com
Sun Apr 10 19:14:13 CEST 2011


On 10/04/2011 13:22, Jean-Pierre M wrote:
 > I created a simple program which writes in a unicode files some 
french text with accents!
[snip]
This line:

     l.p("premier message de Log à accents")

passes a bytestring to the method, and inside the method, this line:

     unicode_str=u'%s : %s \n'  % 
(date_stamp,msg.encode(self.charset_log,'replace'))

it tries to encode the bytestring to Unicode.

It's not possible to encode a bytestring, only a Unicode string, so
Python tries to decode the bytestring using the fallback encoding
(ASCII) and then encode the result.

Unfortunately, the bytestring isn't ASCII (it contains accented
characters), so it can't be decoded as ASCII, hence the exception.

BTW, it's probably better to forget about cp1252, etc, and use UTF-8
instead, and also to use Unicode wherever possible.



More information about the Python-list mailing list