Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > what is best method to set sys.stdout to utf-8?

Reply
Thread Tools

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

 
 
Peter Kleiweg
Guest
Posts: n/a
 
      03-07-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
http://pkleiweg.home.xs4all.nl/
 
Reply With Quote
 
 
 
 
Terry Reedy
Guest
Posts: n/a
 
      03-08-2012
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

 
Reply With Quote
 
 
 
 
Laurent Claessens
Guest
Posts: n/a
 
      03-08-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
 
Reply With Quote
 
Laurent Claessens
Guest
Posts: n/a
 
      03-08-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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the best method to match a pattern in set of lines Terminator Python 9 06-24-2009 05:17 PM
method def in method vs method def in block Kyung won Cheon Ruby 0 11-21-2008 08:48 AM
method names-how to indicate get vs set type of method? Greg Hauptmann Ruby 1 11-06-2008 03:19 AM
Treeview questions - how to set set start node and how to catch click event Alan Silver ASP .Net 0 12-21-2005 10:40 AM
Unable to set focus to textfield in a applet if browser is set to Sun JRE 1.4 Manav Java 0 10-15-2003 03:42 PM



Advertisments