UnicodeEncodeError when piping stdout, but not when printing directly to the console

Adam Funk a24061 at ducksburg.com
Fri Jan 6 09:22:17 EST 2012


On 2012-01-06, Peter Otten wrote:

> Adam Funk wrote:
>
>> On 2012-01-04, Peter Otten wrote:
>> 
>>> Adam Funk wrote:
>> 
>>>> How can I force python (preferably within my python program, rather
>>>> than having to set something externally) to treat stdout as UTF-8?
>>>
>>>
>>> $ cat force_utf8.py
>>> # -*- coding: utf-8 -*-
>>> import sys
>>>
>>> if sys.stdout.encoding is None:
>>>     import codecs
>>>     writer = codecs.getwriter("utf-8")
>>>     sys.stdout = writer(sys.stdout)
>>>
>>> print u"Ähnlich üblich nötig"
>> 
>> That's great, thanks!
>> 
>> I guess issues like this will magically go away when I eventually move
>> to Python 3?
>
> Not "magically", but UTF-8 has become the default encoding...

Close enough!



-- 
When Elaine turned 11, her mother sent her to train under
Donald Knuth in his mountain hideaway.         [XKCD 342]



More information about the Python-list mailing list