Tomasz Toczyski <[EMAIL PROTECTED]> 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 -- http://mail.python.org/mailman/listinfo/python-list