简体   繁体   中英

Python: How to print this special string?

I want to print '\\xd6\\xd0\\xb9\\xfa\\xba\\xda\\xc1\\xfa\\xbd\\xad' which is a Chinese character.

l = ['\xd6\xd0\xb9\xfa\xba\xda\xc1\xfa\xbd\xad']
a = [l[0].decode('utf-8')]
print(a[0])

But it raises this error: UnicodeDecodeError: 'utf8' codec can't decode byte 0xd6 in position 0: invalid continuation byte . I also tried deocde('latin-1') . But the result aren't Chinese characters.

Try with:

l = ['\xd6\xd0\xb9\xfa\xba\xda\xc1\xfa\xbd\xad']
a = [l[0].decode('gb2312').encode('utf-8')]
print(a[0])

output:

中国黑龙江

Update: as Mark's advice, use l[0].decode('gb2312') will be sufficient.

l = ['\xd6\xd0\xb9\xfa\xba\xda\xc1\xfa\xbd\xad']
a = [l[0].decode('gb2312')]
print(a[0])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM