Forcing any output (file / stdout) to UTF-8

N

News123

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top