f



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

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

Ex:
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
'UTF-8'
>>> 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:
"encoding
    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 =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 100,000 Newsgroups - 19 Different Servers! =-----
0
6/7/2004 1:13:05 AM
comp.lang.python 77058 articles. 6 followers. Post Follow

2 Replies
552 Views

Similar Articles

[PageSpeed] 29

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)).

Regards,
Martin

0
UTF
6/7/2004 5:55:00 AM
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(https://sourceforge.net/tracker/?group_id=5470&atid=105470)

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'
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 =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 100,000 Newsgroups - 19 Different Servers! =-----
0
6/7/2004 10:54:00 AM
Reply: