why this is wrong?

John Machin sjmachin at lexicon.net
Mon Oct 9 13:13:43 CEST 2006


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




More information about the Python-list mailing list