I'm having a small python script printing out UTF-8 characters.
# -*- coding: utf-8 -*-
s=u"abcd?fg" # string containing one non ASCII character
# just in case nntp kills it
for c in s:
It work perfectly fine on my utf-8 capable terminal.
However when trying to redirect its output or to just pipe it through
more, then the application fails with
'ascii' codec can't encode character u'\xe9' in position 0: ordinal not
in range(128), as encoding has been switched to none.
because sys.stdout.encoding is now None
The same problem would also occur if I had written directly to a file.
So my qusetion are:
- How can I force the encoding of stdout to a certain encoding. (UTF-8)
- How can I configure the default encoding of any file to be opened to
Thanks for any suggestions or other ideas how you solve the problem.
A current workaround for forcing UTF-8 output for all output to stdout
sys.stdout = UtfStdOut()
but I wonder if there's something better