Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > file.encoding doesn't apply to file.write?

Reply
Thread Tools

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

 
 
Matthew Mueller
Guest
Posts: n/a
 
      06-07-2004
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(12


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! =-----
 
Reply With Quote
 
 
 
 
=?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?=
Guest
Posts: n/a
 
      06-07-2004
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

 
Reply With Quote
 
 
 
 
Matthew Mueller
Guest
Posts: n/a
 
      06-07-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(https://sourceforge.net/tracker/?gro...70&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! =-----
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Wireless Only Desktop - Network Starts too late and AD Computer Policies Don't Apply Kenny Wireless Networking 3 11-08-2005 10:53 PM
How to apply xml commentig code salvatore.difazio@gmail.com ASP .Net 3 09-30-2005 06:22 AM
[Vb.net question] how to apply online update function into program (the effect just like Norton system work live update) chan ASP .Net 1 03-04-2004 02:58 PM
Testbench HowTo Apply Hex Values from a File Markus Meng VHDL 1 01-15-2004 06:45 PM
[XSLT] could not apply "apply-templates" Stefan Siegl XML 1 07-18-2003 09:43 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57