why this is wrong?

Fredrik Lundh fredrik at pythonware.com
Mon Oct 9 06:46:20 EDT 2006


"bruce.who.hk" wrote:

> 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)

the str() function is supposed to return an 8-bit string, so when you return a
Unicode string instead, it attempts to convert it using the default encoding.

try using the unicode() function instead.

</F> 






More information about the Python-list mailing list