Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.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 UnicodeDecodeError:&x27;utf-8';编解码器可以';位置0处的t解码字节0x9d:执行'b.decode()时开始字节无效`_Python_Character Encoding - Fatal编程技术网

Python UnicodeDecodeError:&x27;utf-8';编解码器可以';位置0处的t解码字节0x9d:执行'b.decode()时开始字节无效`

Python UnicodeDecodeError:&x27;utf-8';编解码器可以';位置0处的t解码字节0x9d:执行'b.decode()时开始字节无效`,python,character-encoding,Python,Character Encoding,我有一个类似于贝娄的代码: cn_bytes = [157, 188, 156] cn_str = "" clen = len(cn_bytes) count = int(clen / 3) for x in range(count): i = x * 3 b = bytes([cn_bytes[i], cn_bytes[i + 1], cn_bytes[i + 2]]) print(b) cn_str += b.decode() 当我执行它时,我将得到以

我有一个类似于贝娄的代码:

cn_bytes = [157, 188, 156]

cn_str = ""
clen = len(cn_bytes)
count = int(clen / 3)
for x in range(count):
    i = x * 3
    b = bytes([cn_bytes[i], cn_bytes[i + 1], cn_bytes[i + 2]])

    print(b)

    cn_str += b.decode()
当我执行它时,我将得到以下错误:

Traceback (most recent call last):
  File "/Users/dele/Desktop/TestIOS/TestPython/testDemo01/testDemo01/test08.py", line 30, in <module>
    cn_str += b.decode()
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x9d in position 0: invalid start byte
回溯(最近一次呼叫最后一次):
文件“/Users/dele/Desktop/TestIOS/TestPython/testDemo01/testDemo01/test08.py”,第30行,在
cn_str+=b.decode()
UnicodeDecodeError:“utf-8”编解码器无法解码位置0中的字节0x9d:无效的开始字节
有人说:

UnicodeDecodeError:“utf-8”编解码器无法解码位置0中的字节0x9d:无效的开始字节


实际上,我认为不能使用
bytes()
将整个列表转换为字节。但您仍然可以通过迭代各个元素将其转换为字节

b = [cn_bytes[i], cn_bytes[i + 1], cn_bytes[i + 2]]

cn_str =  [bytes(i)for i in b]

我想得到
cn\u str
,但是b.decode()现在得到了错误。事实上,这是我的一部分,当我在那里运行websocket服务器时,我得到了这个错误。