why this is wrong?

B

bruce.who.hk

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?
 
J

John Machin

bruce.who.hk said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top