473,480 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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! =-----
Jul 18 '05 #1
2 1806
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

Jul 18 '05 #2
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! =-----
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
3417
by: Dario de Judicibus | last post by:
I need to read a file as BINARY by 16 bits units, and write to another file as BINARY by 8, 16 and 24 bits units. It is pratically a pipe. I'm looking for a sample of C++ code simple, performing...
8
1912
by: Siu | last post by:
Hi, is it possible to sort a XML file without using a XSLT or XSL file?? For example, my XML is: <A> <1> <3> <4> <2> </A>
3
4622
by: tigrrgrr42 | last post by:
I am working(vb.net03and05) with word documents stored in a sql db and I am currently bringing them from a byte array into a temp file to pop into word and make word do its thing as a com object. ...
11
3203
by: utabintarbo | last post by:
Here is my situation: I am trying to programatically access files created on an IBM AIX system, stored on a Sun OS 5.8 fileserver, through a samba-mapped drive on a Win32 system. Not confused?...
5
3633
by: philip | last post by:
Here is some lines of code than I wrote. You can copy/paste theis code as code of form1 in a new project. My problem is this one : I try to write in a file a serie of bytes. BUT some bytes...
2
2339
by: jkflens | last post by:
Hello, i convert one XML-document by using XSLT into another XML-document. First change all attributes to elements is no problem. Then i try to insert a new element into the new document by...
1
6431
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
3
3143
by: shapper | last post by:
Hello, Could someone tell me how to convert a XML file into another XML file using a XSL file with a parameter? I created the code to do it, and it seems ok, but it is not working. Could...
1
32841
by: ujjwaltrivedi | last post by:
Hey guys, Can anyone tell me how to create a text file with Unicode Encoding. In am using FileStream Finalfile = new FileStream("finalfile.txt", FileMode.Append, FileAccess.Write); ...
0
7034
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7076
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6732
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6886
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5324
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
1
4768
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
2990
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1294
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
174
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.