Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #21363

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

From Terry Reedy <tjreedy@udel.edu>
Subject Re: what is best method to set sys.stdout to utf-8?
Date 2012-03-07 19:12 -0500
References <alpine.DEB.2.00.1203072150450.2657@pebbe>
Newsgroups comp.lang.python
Message-ID <mailman.497.1331165710.3037.python-list@python.org> (permalink)

Show all headers | View raw


On 3/7/2012 3:57 PM, Peter Kleiweg wrote:
>
> 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?

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.

-- 
Terry Jan Reedy

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

what is best method to set sys.stdout to utf-8? Peter Kleiweg <pkleiweg@xs4all.nl> - 2012-03-07 21:57 +0100
  Re: what is best method to set sys.stdout to utf-8? Terry Reedy <tjreedy@udel.edu> - 2012-03-07 19:12 -0500
    Re: what is best method to set sys.stdout to utf-8? Laurent Claessens <moky.math@gmail.com> - 2012-03-08 07:45 +0100
    Re: what is best method to set sys.stdout to utf-8? Laurent Claessens <moky.math@gmail.com> - 2012-03-08 07:45 +0100

csiph-web