Unicode conversion in 'print'
Serge Orlov
Serge.Orlov at gmail.com
Thu Jan 13 17:33:20 EST 2005
Ricardo Bugalho wrote:
> Hello,
> I'm using Python 2.3.4 and I noticed that, when stdout is a
terminal,
> the 'print' statement converts Unicode strings into the encoding
> defined by the locales instead of the one returned by
> sys.getdefaultencoding().
Sure. It uses the encoding of you console. Here is explanation why it
uses locale to get the encoding of console:
http://www.python.org/moin/PrintFails
> However, I can't find any references to it. Anyone knows where it's
> descrbed?
I've just wrote about it here:
http://www.python.org/moin/DefaultEncoding
>
> Example:
>
> !/usr/bin/env python
> # -*- coding: utf-8 -*-
>
> import sys, locale
>
> print 'Python encoding:', sys.getdefaultencoding()
> print 'System encoding:', locale.getpreferredencoding()
> print 'Test string: ', u'Olá mundo'
>
>
> If stdout is a terminal, works fine
> $ python x.py
> Python encoding: ascii
> System encoding: UTF-8
> Test string: Olá mundo
>
> If I redirect the output to a file, raises an UnicodeEncodeError
exception
> $ python x.py > x.txt
> Traceback (most recent call last):
> File "x.py", line 8, in ?
> print 'Test string: ', u'Olá mundo'
> UnicodeEncodeError: 'ascii' codec can't encode character u'\xe1' in
position 2: ordinal not in range(128)
>
http://www.python.org/moin/ShellRedirectionFails
Feel free to reply here if something is not clear, corrections in wiki
are also welcome.
Serge.
More information about the Python-list
mailing list