Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #21342 > unrolled thread
| Started by | Peter Kleiweg <pkleiweg@xs4all.nl> |
|---|---|
| First post | 2012-03-07 21:57 +0100 |
| Last post | 2012-03-08 07:45 +0100 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.lang.python
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
| From | Peter Kleiweg <pkleiweg@xs4all.nl> |
|---|---|
| Date | 2012-03-07 21:57 +0100 |
| Subject | what is best method to set sys.stdout to utf-8? |
| Message-ID | <alpine.DEB.2.00.1203072150450.2657@pebbe> |
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/
[toc] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2012-03-07 19:12 -0500 |
| Message-ID | <mailman.497.1331165710.3037.python-list@python.org> |
| In reply to | #21342 |
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
[toc] | [prev] | [next] | [standalone]
| From | Laurent Claessens <moky.math@gmail.com> |
|---|---|
| Date | 2012-03-08 07:45 +0100 |
| Message-ID | <4F58558C.3000706@gmail.com> |
| In reply to | #21363 |
> 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
[toc] | [prev] | [next] | [standalone]
| From | Laurent Claessens <moky.math@gmail.com> |
|---|---|
| Date | 2012-03-08 07:45 +0100 |
| Message-ID | <mailman.503.1331189452.3037.python-list@python.org> |
| In reply to | #21363 |
> 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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web