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


Groups > comp.lang.python > #18604

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

From Adam Funk <a24061@ducksburg.com>
Newsgroups comp.lang.python
Subject Re: UnicodeEncodeError when piping stdout, but not when printing directly to the console
Date 2012-01-06 14:22 +0000
Organization $CABAL
Message-ID <plcjt8xuso.ln2@news.ducksburg.com> (permalink)
References <l4set8xjf7.ln2@news.ducksburg.com> <mailman.4430.1325713810.27778.python-list@python.org> <mgqit8xu0g.ln2@news.ducksburg.com> <mailman.4475.1325842466.27778.python-list@python.org>

Show all headers | View raw


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]

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


Thread

UnicodeEncodeError when piping stdout, but not when printing directly to the console Adam Funk <a24061@ducksburg.com> - 2012-01-04 21:15 +0000
  Re: UnicodeEncodeError when piping stdout, but not when printing directly to the console Peter Otten <__peter__@web.de> - 2012-01-04 22:49 +0100
    Re: UnicodeEncodeError when piping stdout, but not when printing directly to the console Adam Funk <a24061@ducksburg.com> - 2012-01-06 09:12 +0000
      Re: UnicodeEncodeError when piping stdout, but not when printing directly to the console Peter Otten <__peter__@web.de> - 2012-01-06 10:34 +0100
        Re: UnicodeEncodeError when piping stdout, but not when printing directly to the console Adam Funk <a24061@ducksburg.com> - 2012-01-06 14:22 +0000

csiph-web