468,287 Members | 1,968 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,287 developers. It's quick & easy.

why this is wrong?

Hi, all

I just donnot know why this is wrong, you can test it in python shell:

class B:
def __str__(self):
return u'\u5929\u4e0b'

b=B()
str(b)
Traceback (most recent call last):
File "<input>", line 1, in ?
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)

Could anybody point me out?

--------------
bruce.who.hk
2006-10-09

Oct 9 '06 #1
1 1239
bruce.who.hk wrote:
Hi, all

I just donnot know why this is wrong, you can test it in python shell:

class B:
def __str__(self):
return u'\u5929\u4e0b'

b=B()
str(b)
Traceback (most recent call last):
File "<input>", line 1, in ?
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)
str(b) is trying to convert your unicode string to a str (8-bit)
string. You didn't tell it how to convert it. So it assumed the
default: ascii. The tens of thousands of Chinese characters can't be
encoded in the 128 ascii possibilities. So you got an error message.

You need to encode it with an encoding that (1) accomodates the Chinese
characters that you want to use *and* (2) can be rendered properly on
your screen.

Try these:
b.encode('big5')
# but you may need b.encode('big5hkscs')
b.encode('gb18030')
b.encode('utf_8')

I hope this helps you.
Regards,
John

Oct 9 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

21 posts views Thread by Jay Levitt | last post: by
7 posts views Thread by Jerry Krinock | last post: by
5 posts views Thread by Krisnamourt Correia via SQLMonster.com | last post: by
6 posts views Thread by Michael Sparks | last post: by
3 posts views Thread by Soren Jorgensen | last post: by
8 posts views Thread by Dmitry Korolyov | last post: by
3 posts views Thread by belton180 | last post: by
318 posts views Thread by jacob navia | last post: by
3 posts views Thread by Siong.Ong | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.