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


Groups > comp.lang.python > #2953 > unrolled thread

Re: Encoding problem when launching Python27 via DOS

Started byMRAB <python@mrabarnett.plus.com>
First post2011-04-10 18:14 +0100
Last post2011-04-10 18:14 +0100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#2953 — Re: Encoding problem when launching Python27 via DOS

FromMRAB <python@mrabarnett.plus.com>
Date2011-04-10 18:14 +0100
SubjectRe: Encoding problem when launching Python27 via DOS
Message-ID<mailman.194.1302455663.9059.python-list@python.org>
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.

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web