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

Matthew Mueller doNOSPAMnut at
Mon Jun 7 03:13:05 CEST 2004

I noticed in python2.3 printing unicode to an appropriate terminal
actually works.  But using sys.stdout.write doesn't.

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.
>>> sys.stdout.encoding
>>> u=u'\u3053\u3093\u306b\u3061\u308f'
>>> print u
>>> sys.stdout.write(u)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-4: ordinal not in range(128)

The file object docs say:
    The encoding that this file uses. When Unicode strings are written to
    a file, they will be converted to byte strings using this encoding.
Which indicates to me that it is supposed to work.

Of course, I could use print >>fileobj, but that is ugly ;)

-----= 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