Setting stdout encoding

Ryan Ginstrom software at ginstrom.com
Thu Sep 13 07:43:11 EDT 2007


> On Behalf Of Fabio Zadrozny
> Makes sense... Do you think that creating a new object, 
> setting it as sys.stdout and overriding its write() method to 
> check for a unicode string to do 
> original_stdout.write(unicode_str.encode(my_encoding)) would 
> do it?

Here's an output stream encoder I have used. It might be kind of screwball,
so I'd welcome any feedback on it, but it does work for encoding output
streams.

import sys

class OutStreamEncoder(object):
    """Wraps an out stream with an encoder"""

    def __init__(self, outstream, encoding=None):
        self.stdout = outstream
        if not encoding:
            self.encoding = sys.getfilesystemencoding()
        else:
            self.encoding = encoding

    def write(self, obj):
        """Wraps the output stream's write method, encoding it with
        the specified encoding"""

        self.stdout.write(obj.encode(self.encoding))

    def __getattr__(self, attr):
        """Delegate everything but write to the stream"""

        if attr != "write":
            return getattr(self.stdout, attr)
        return self.write


>>> from cStringIO import StringIO as si
>>> out = si()
>>> nihongo = unicode("日本語", "sjis")
>>> print >> out, nihongo

Traceback (most recent call last):
  File "<pyshell#40>", line 1, in <module>
    print >> out, nihongo
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2:
ordinal not in range(128)
>>> out = OutStreamEncoder(out, "utf-8")
>>> print >> out, nihongo
>>> val = out.getvalue()
>>> print val.decode("utf-8")
日本語

>>> 

Regards,
Ryan Ginstrom




More information about the Python-list mailing list