why this is wrong?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bruce.who.hk

    #1

    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 ?
    UnicodeEncodeEr ror: '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

  • John Machin

    #2
    Re: why this is wrong?

    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 ?
    UnicodeEncodeEr ror: '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('big5h kscs')
    b.encode('gb180 30')
    b.encode('utf_8 ')

    I hope this helps you.
    Regards,
    John

    Comment

    Working...