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

Discussion in 'Python' started by Peter Kleiweg, Mar 7, 2012.

  1. 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
    http://pkleiweg.home.xs4all.nl/
    Peter Kleiweg, Mar 7, 2012
    #1
    1. Advertising

  2. Peter Kleiweg

    Terry Reedy Guest

    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
    Terry Reedy, Mar 8, 2012
    #2
    1. Advertising


  3. > 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
    Laurent Claessens, Mar 8, 2012
    #3

  4. > 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
    Laurent Claessens, Mar 8, 2012
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roman Neuhauser
    Replies:
    0
    Views:
    643
    Roman Neuhauser
    Apr 4, 2005
  2. Replies:
    2
    Views:
    637
    velle
    Jan 5, 2006
  3. Replies:
    0
    Views:
    446
  4. Mitchell L Model

    sys.stdout vs. sys.stderr

    Mitchell L Model, Jan 11, 2010, in forum: Python
    Replies:
    2
    Views:
    552
    Nobody
    Jan 11, 2010
  5. Michel Albert
    Replies:
    5
    Views:
    1,353
    Daniel Dehennin
    Oct 24, 2012
Loading...

Share This Page