pipeline encoding
Rob Wolfe
rw at smsnet.pl
Thu Dec 6 12:57:50 EST 2007
Tomasz Toczyski <tt at praterm.com.pl> writes:
> 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?
If you print to a terminal Python can use terminal encoding,
but if you redirect to a file Python doesn't know
what encoding to use (e.g. how was encoded existing file)
and refuses to guess.
You have to specify that encoding explicit:
python -c "print u'\u03A9'.encode('utf-8')" > file.txt
HTH,
Rob
More information about the Python-list
mailing list