473,394 Members | 1,761 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,394 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 3103
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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
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...
0
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...

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.