Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #2953

Re: Encoding problem when launching Python27 via DOS

Date 2011-04-10 18:14 +0100
From MRAB <python@mrabarnett.plus.com>
Subject Re: Encoding problem when launching Python27 via DOS
References <BANLkTimhkTq_bFSyzUaG9wNWnLwjc16P2A@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.194.1302455663.9059.python-list@python.org> (permalink)

Show all headers | View raw


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.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Encoding problem when launching Python27 via DOS MRAB <python@mrabarnett.plus.com> - 2011-04-10 18:14 +0100

csiph-web