En Thu, 06 Dec 2007 14:13:04 -0300, Tomasz Toczyski <tt@praterm.com.pl>
escribió:
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.
Because in this case stdout is bound to your terminal and Python can ask
the OS which encoding it uses.
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)
In this case, stdout is redirected, and a file can be written in any
encoding you like. So unless you tell Python which encoding to use, it
refuses to guess. Try:
python -c "print u'\u03A9'.encode('utf-8')" file.txt
Also try: python -c "import sys; print sys.stdout.encoding"
and see what happens in both cases.
--
Gabriel Genellina