Forcing any output (file / stdout) to UTF-8
News123
news1234 at free.fr
Sun Jun 6 10:53:19 EDT 2010
Hi,
I'm having a small python script printing out UTF-8 characters.
# -*- coding: utf-8 -*-
print sys.stdout.encoding
s=u"abcdéfg" # string containing one non ASCII character
# just in case nntp kills it
for c in s:
print c
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
UTF-8?
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
could be:
class UtfStdOut():
def write(self,data):
sys.__stdout__.write(data.encode('UTF-8'))
sys.stdout = UtfStdOut()
but I wonder if there's something better
More information about the Python-list
mailing list