473,287 Members | 1,964 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

print vs sys.stdout.write, and UnicodeError

Greetings,

I have observed the following (python 2.5.1):
>>import sys
print sys.stdout.encoding
UTF-8
>>print(u'\u00e9')
é
>>sys.stdout.write(u'\u00e9\n')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in
position 0: ordinal not in range(128)

Is this correct? My understanding is that print ultimately calls
sys.stdout.write anyway, so I'm confused as to why the Unicode error
occurs in the second case. Can someone explain?

Thanks,

Brent
Oct 25 '07 #1
2 3099
25 Oct 2007 17:37:01 GMT, Brent Lievers <3w**@qlink.queensu.ca>:
Greetings,

I have observed the following (python 2.5.1):
>import sys
print sys.stdout.encoding
UTF-8
>print(u'\u00e9')
é
>sys.stdout.write(u'\u00e9\n')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in
position 0: ordinal not in range(128)
>>sys.stdout.write(u'\u00e9\n'.encode("UTF-8"))
é
Is this correct? My understanding is that print ultimately calls
sys.stdout.write anyway, so I'm confused as to why the Unicode error
occurs in the second case. Can someone explain?
you forgot to encode what you are going to "print" :)

--
http://noneisyours.marcher.name
http://feeds.feedburner.com/NoneIsYours
Oct 25 '07 #2
Martin Marcher <ma****@marcher.namewrote:
25 Oct 2007 17:37:01 GMT, Brent Lievers <3w**@qlink.queensu.ca>:
>Greetings,

I have observed the following (python 2.5.1):
>>import sys
print sys.stdout.encoding
UTF-8
>>print(u'\u00e9')
?
>>sys.stdout.write(u'\u00e9\n')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in
position 0: ordinal not in range(128)
>>>sys.stdout.write(u'\u00e9\n'.encode("UTF-8"))
?
>Is this correct? My understanding is that print ultimately calls
sys.stdout.write anyway, so I'm confused as to why the Unicode error
occurs in the second case. Can someone explain?

you forgot to encode what you are going to "print" :)
Thanks. I obviously have a lot to learn about both Python and Unicode ;-)

So does print do this encoding step based on the value of
sys.stdout.encoding? In other words, something like:

sys.stdout.write(textstr.encode(sys.stdout.encodin g))

I'm just trying to understand why encode() is needed in the one case but
not the other.

Cheers,

Brent

Oct 25 '07 #3

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

Similar topics

19
by: Karl Scalet | last post by:
Hi, quite often there is a need to just print out the items of a list. that would be nice, except prt() does not exist, and print is a statement not a function, hence cannot replace prt as...
30
by: Martin Bless | last post by:
Why can't we have an additional 'print' statement, that behaves exactly like 'print' but doesn't insert that damn blank between two arguments? Could be called 'printn' or 'prin1' or 'prinn'...
5
by: vegetax | last post by:
How can i use cgi'like print statement in a multitreaded web framework? each thread has its own Servlet instance with request/response objects, sys.stdout = self.response(which is a file like...
4
by: Ju Hui | last post by:
I want to print 3 numbers without blank. >>> for x in range(3): .... print x .... 0 1 2 >>> for x in range(3): .... print x, ....
11
by: A.M | last post by:
Hi, I found print much more flexible that write method. Can I use print instead of file.write method? Thank you,
2
by: Phoe6 | last post by:
print and softspace in python In python, whenever you use >>>print statement it will append a newline by default. If you don't want newline to be appended, you got use a comma at the end (>>>print...
4
by: MisterPete | last post by:
I created an object that inherits from file and was a bit surprised to find that print seems to bypass the write method for objects inheriting from file. An optimization I suppose. Does this...
6
by: pirata | last post by:
I was trying to print a dot on console every second to indicates running process, so I wrote, for example: for i in xrange(10): print ".", time.sleep(1) Idealy, a dot will be printed out...
7
by: bukzor | last post by:
I was trying to change the behaviour of print (tee all output to a temp file) by inheriting from file and overwriting sys.stdout, but it looks like print uses C-level stuff to do its writes which...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.