Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.dougwise.org!news.le-studio75.com!news.univ-fcomte.fr!not-for-mail From: Laurent Claessens Newsgroups: comp.lang.python Subject: Re: the stupid encoding problem to stdout Date: Fri, 10 Jun 2011 07:47:46 +0200 Organization: A poorly-installed InterNetNews site Lines: 66 Message-ID: <4DF1B002.7070507@gmail.com> References: <4df02e04$0$1779$a729d347@news.telepac.pt> NNTP-Posting-Host: soleil.univ-fcomte.fr Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: news.univ-fcomte.fr 1307684862 3545 194.57.84.1 (10 Jun 2011 05:47:42 GMT) X-Complaints-To: news@news.univ-fcomte.fr NNTP-Posting-Date: Fri, 10 Jun 2011 05:47:42 +0000 (UTC) To: =?UTF-8?B?U8OpcmdpbyBNb250ZWlybyBCYXN0bw==?= User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.13) Gecko/20101208 Thunderbird/3.1.7 In-Reply-To: <4df02e04$0$1779$a729d347@news.telepac.pt> Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:7354 Le 09/06/2011 04:18, Sérgio Monteiro Basto a écrit : > hi, > cat test.py > #!/usr/bin/env python > #-*- coding: utf-8 -*- > u = u'moçambique' > print u.encode("utf-8") > print u > > chmod +x test.py > ../test.py > moçambique > moçambique The following tries to encode before to print. If you pass an already utf-8 object, it just print it; if not it encode it. All the "print" statements pass by MyPrint.write #!/usr/bin/env python #-*- coding: utf-8 -*- import sys class MyPrint(object): def __init__(self): self.old_stdout=sys.stdout sys.stdout=self def write(self,text): try: encoded=text.encode("utf8") except UnicodeDecodeError: encoded=text self.old_stdout.write(encoded) MyPrint() u = u'moçambique' print u.encode("utf-8") print u TEST : $ ./test.py moçambique moçambique $ ./test.py > test.txt $ cat test.txt moçambique moçambique By the way, my code will not help for error message. I think that the errors are printed by sys.stderr.write. So if you want to do raise "moçambique" you should think about add stderr to the class MyPrint If you know French, I strongly recommend "Comprendre les erreurs unicode" by Victor Stinner : http://dl.afpy.org/pycon-fr-09/Comprendre_les_erreurs_unicode.pdf Have a nice day Laurent