gpt4 book ai didi

python - Unicode解码错误: 'utf-8' codec can't decode byte 0x9d in position 0: invalid start byte when I execute the ` b.decode()`

转载 作者:行者123 更新时间:2023-12-01 07:53:40 26 4
gpt4 key购买 nike

我有如下代码:

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

那里说:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x9d in position 0: invalid start byte

最佳答案

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

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

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

关于python - Unicode解码错误: 'utf-8' codec can't decode byte 0x9d in position 0: invalid start byte when I execute the ` b.decode()` ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56074317/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com