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

Peter Kleiweg pkleiweg at xs4all.nl
Wed Mar 7 21:57:03 CET 2012

In Python 3, there seem to be two ways to set sys.stdout to 
utf-8 after the script has started:

    sys.stdout = codecs.getwriter('utf-8')(sys.stdout.detach())

    sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding='utf-8')

I guess the second is better. At start-up, type(sys.stdout) is 
<class '_io.TextIOWrapper'>, and it's also after using the 
second method.

After using the first method, type(sys.stdout) is changed to 
<class 'encodings.utf_8.StreamWriter'>. 

Should I always use the second method?

Peter Kleiweg

More information about the Python-list mailing list