我需要解码一个“UNICODE”编码的字符串:
>>> id = u'abcdß'
>>> encoded_id = id.encode('utf-8')
>>> encoded_id
'abcd\xc3\x9f'
我遇到的问题是: 使用Pylons路由,我得到的编码后的id变量为一个Unicode字符串
u'abcd\xc3\x9f'
,而不是一个普通字符串'abcd\xc3\x9f'
:在Python中,如何解码我的编码后的id变量,它是一个Unicode字符串?
>>> encoded_id = u'abcd\xc3\x9f'
>>> encoded_id.decode('utf-8')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/test/vng/lib64/python2.6/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 4-5: ordinal not in range(128)