file.encoding doesn't apply to file.write?

Matthew Mueller doNOSPAMnut at
Mon Jun 7 12:54:00 CEST 2004

On Mon, 07 Jun 2004 07:55:00 +0200, Martin v. Löwis wrote:

> Matthew Mueller wrote:
>> I noticed in python2.3 printing unicode to an appropriate terminal
>> actually works.  But using sys.stdout.write doesn't.
> Please report that as a bug. As a work-around, explicitly encode
> with sys.stdout.encoding (or make a codecs.StreamReaderWriter,
> passing codecs.lookup(sys.stdout.encoding)).

I submitted a bug(

And I'm trying using StreamWriter, which I may actually want anyway so I
can set the error handling.  But I've ran into a weird thing.  Some codecs
don't like writing strings, only unicode.  This is problematic because it
means I can't just use the StreamWriter as a drop in replacement for
stdout, etc:

Python 2.3.4 (#2, May 29 2004, 03:31:27) 
[GCC 3.3.3 (Debian 20040417)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import codecs
>>> sys.stdout=codecs.getwriter('UTF-8')(sys.__stdout__)
>>> print 'hello'
>>> sys.stdout=codecs.getwriter('EUC-JP')(sys.__stdout__)
>>> print 'hello'
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: only unicode objects are encodable.

-----= Posted via Newsfeeds.Com, Uncensored Usenet News =----- - The #1 Newsgroup Service in the World!
-----==  Over 100,000 Newsgroups - 19 Different Servers! =-----

More information about the Python-list mailing list