【发布时间】:2018-03-08 14:02:00
【问题描述】:
我有一个字节串b"\xDF"。当我尝试将其解码为 UTF-8 时,会抛出 UnicodeDecodeError。解码为 CP1252 工作正常。在这两个字符集中,0xDF 由字符“ß”表示。那么为什么会出现错误呢?
>>> hex(ord("ß"))
'0xdf'
>>> b"\xDF".decode("utf-8")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xdf in position 0: unexpected end of data
>>> b"\xDF".decode("cp1252")
'ß'
【问题讨论】:
标签: python-3.x unicode utf-8 cp1252