Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'passes': 0.05; 'string,': 0.05; 'ascii': 0.07; 'method,': 0.07; 'subject:when': 0.07; 'python': 0.07; '(it': 0.09; 'exception.': 0.09; 'from:addr:python': 0.09; 'line:': 0.09; 'utf-8': 0.09; 'result.': 0.14; 'wrote:': 0.14; 'ascii,': 0.16; 'fallback': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'received:84.92': 0.16; 'received:84.92.122': 0.16; 'received:84.92.122.60': 0.16; 'reply- to:addr:python-list': 0.16; 'skip:( 50': 0.16; 'tries': 0.16; 'header:In-Reply-To:1': 0.22; 'etc,': 0.23; 'received:84': 0.25; 'subject:problem': 0.25; 'instead,': 0.29; 'unicode': 0.29; 'probably': 0.30; 'btw,': 0.31; "can't": 0.31; 'to:addr:python- list': 0.32; 'created': 0.33; "isn't": 0.34; 'using': 0.34; 'header:User-Agent:1': 0.35; 'reply-to:addr:python.org': 0.35; 'some': 0.37; 'files': 0.38; 'log': 0.38; 'to:addr:python.org': 0.39; "it's": 0.40; 'simple': 0.60; 'french': 0.67; 'reply-to:no real name:2**0': 0.72; 'header:Reply-To:1': 0.72 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AlsIADvloU3Unw4S/2dsb2JhbACZA40Zd78AhW4EkUI Date: Sun, 10 Apr 2011 18:14:13 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.2.15) Gecko/20110303 Thunderbird/3.1.9 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Encoding problem when launching Python27 via DOS References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: python-list@python.org List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 24 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1302455663 news.xs4all.nl 81478 [::ffff:82.94.164.166]:53210 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:2953 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.