FAQ
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

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJun 6, '10 at 2:53p
activeJun 6, '10 at 2:53p
posts1
users1
websitepython.org

1 user in discussion

News123: 1 post

People

Translate

site design / logo © 2022 Grokbase