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

  • Thread starter Matthew Mueller
  • Start date
M

Matthew Mueller

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

Guest

Matthew said:
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
 
M

Matthew Mueller

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.Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: only unicode objects are encodable.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top