views:

120

answers:

1

I'm having a problem regarding Unicode in Python. I can print the output fine in a regular terminal, but if I redirect stdout elsewhere (or capture it with the subprocess module), I get a UnicodeEncodeError:

$ cat example.py 
print u'Example: \u00F1'
$ python example.py 
Example: ñ
$ python example.py > /dev/null
Traceback (most recent call last):
  File "example.py", line 1, in <module>
    print u'Example: \u00F1'
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf1' in position 9: ordinal not in range(128)

Why is this? How can I fix it?

+2  A: 

Pipes that don't lead to the terminal don't have an encoding, therefore you'll need to check sys.stdout.isatty() and encode if needed.

Ignacio Vazquez-Abrams
Or (even better) just always encode.
Thomas Wouters
More info on how to define encoding: http://www.python.org/dev/peps/pep-0263/
jcoon
No, PEP 263 is about specifying *source* encoding, and has nothing to do with output encodings.
Thomas Wouters