471,328 Members | 1,780 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,328 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 2993
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

19 posts views Thread by Karl Scalet | last post: by
30 posts views Thread by Martin Bless | last post: by
4 posts views Thread by Ju Hui | last post: by
11 posts views Thread by A.M | last post: by
2 posts views Thread by Phoe6 | last post: by
6 posts views Thread by pirata | last post: by
7 posts views Thread by bukzor | last post: by

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.