By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,035 Members | 1,995 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,035 IT Pros & Developers. It's quick & easy.

why this is wrong?

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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.