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

Adam Funk a24061 at ducksburg.com
Wed Jan 4 16:15:33 EST 2012

(I'm using Python 2.7.2+ on Ubuntu.)

When I'm running my program in an xterm, the print command with an
argument containing unicode works fine (it correctly detects my UTF-8
environment).  But when I run it with a pipe or redirect to a file (|
or >), unicode strings fail with the following (for example):

UnicodeEncodeError: 'ascii' codec can't encode character u'\u0107' in position 21: ordinal not in range(128)

How can I force python (preferably within my python program, rather
than having to set something externally) to treat stdout as UTF-8?


Nam Sibbyllam quidem Cumis ego ipse oculis meis vidi in ampulla 
pendere, et cum illi pueri dicerent: beable beable beable; respondebat 
illa: doidy doidy doidy.                                   [plorkwort]

More information about the Python-list mailing list