- 論壇徽章:
- 0
|
有一個(gè)bytes數(shù)據(jù)
a = b'abc\xA0\xA2\xF0'
想要將其轉(zhuǎn)換成字串
b = 'abc\xA0\xA2\xF0'
用decode是肯定不行的,會(huì)報(bào)錯(cuò)
>>> b = a.decode('utf8')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.2/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa0 in position 3: invalid start byte
我試著用逐字節(jié)的方法
>>> for c in a:
... b = b+chr(c)
...
>>> b
'abc\xa0¢ð'
此時(shí),
>>> len(b)
6
長(zhǎng)度仍是6, 但是數(shù)據(jù)是錯(cuò)的
將其再encode就會(huì)發(fā)現(xiàn)
>>> b.encode()
b'abc\xc2\xa0\xc2\xa2\xc3\xb0'
各位有沒(méi)有什么好的辦法
|
|