pipeline encoding

Diez B. Roggisch deets at nospam.web.de
Thu Dec 6 12:56:16 EST 2007


Tomasz Toczyski schrieb:
> My locale is set to UTF-8. The command:
> python -c "print u'\u03A9'"
> gives me the desired result and doesn't produce any error.
> 
> But when I want to redirect the output to a file I invoke:
> python -c "print u'\u03A9'" > file.txt
> I get an error:
> 
> File "<string>", line 1, in <module>
> UnicodeEncodeError: 'ascii' codec can't encode character u'\u03a9' in 
> position 0: ordinal not in range(128)
> 
> How to cope with it?

Python tries and guesses the stdout-encoding based on the terminal 
settings. So the first print works.

However, piping to a file means that it can't do so, because it doesn't 
  (and shouldn't) make any assumptions on the output encoding desired - 
after all, it might be appending to a XML-file with e.g. latin1 encoding.

So you need to explictely encode the unicode-object with the desired 
encoding:


python -c "print u'\u03A9'.encode('utf-8')" > file.txt


Diez



More information about the Python-list mailing list