Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 为什么可以';我能把\xDF(ß;)解码成UTF-8吗?_Python 3.x_Unicode_Utf 8_Cp1252 - Fatal编程技术网

Python 3.x 为什么可以';我能把\xDF(ß;)解码成UTF-8吗?

Python 3.x 为什么可以';我能把\xDF(ß;)解码成UTF-8吗?,python-3.x,unicode,utf-8,cp1252,Python 3.x,Unicode,Utf 8,Cp1252,我有一个bytestringb“\xDF”。当我尝试将其解码为UTF-8时,会抛出一个UnicodeDecodeError。对CP1252的解码工作正常。在这两个字符集中,0xDF由字符“ß”表示。那么为什么会出现错误呢 >>> hex(ord("ß")) '0xdf' >>> b"\xDF".decode("utf-8") Traceback (most recent call last): File "<stdin>", line 1, i

我有一个bytestring
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")
'ß'
十六进制 “0xdf” >>>b“\xDF”。解码(“utf-8”) 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 UnicodeDecodeError:“utf-8”编解码器无法解码位置0中的字节0xdf:数据意外结束 >>>b“\xDF”。解码(“cp1252”) 'ß'
UTF-8中的所有单字节编码字符必须在[0x00..0x7F]()范围内。它们相当于7位ASCII码

对于德语
ß
,您将得到2个UTF-8字节:

>>> "ß".encode("utf-8")
b'\xc3\x9f'

解码时也能正常工作:

b'\xc3\x9f'.decode("utf-8")
ß