Tomek Toczyski wrote: > Diez B. Roggisch: > >> >> 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 > > Thanks. It is a solutiona to my problem but: > > Are there any command line option for telling python what encoding to use > for stdout? > > To be honest I have a more complicated program than the example that I > have presented - there are many print commands inside and it is not very > feasible for me to put .encode('utf-8') inside every print occurence.
Alternatively you can wrap stdout: # -*- coding: utf-8 -*- import sys if sys.stdout.encoding is None: import locale import codecs encoding = locale.getpreferredencoding() # or just "utf-8" streamwriter = codecs.lookup(encoding).streamwriter sys.stdout = streamwriter(sys.stdout) print u"ähnlich üblich möglich" -- http://mail.python.org/mailman/listinfo/python-list