what is best method to set sys.stdout to utf-8?

Laurent Claessens moky.math at gmail.com
Thu Mar 8 01:45:32 EST 2012


> I would. The io module is more recent an partly replaces codecs. The
> latter remains for back compatibility and whatever it can do that io cannot.


  I've a naive question : what is wrong with the following system ?

class MyStdOut(object):
     def __init__(self):
         self.old_stdout=sys.stdout
     def write(self,x):
         try:
             if isinstance(x,unicode):
                 x=x.encode("utf8")
         except (UnicodeEncodeError,UnicodeDecodeError):
             sys.stderr.write("This should not happen !")
             raise
         self.old_stdout.write(x)
sys.stdout=MyStdOut()


... well ... a part of the fact that it is much longer ?


Laurent Claessens



More information about the Python-list mailing list