目前我有一个由我的Python代码接收的字符串的字节流,必须转换为字符串。现在我设法提取每个字符,转换它们并将它们单独附加到字符串。代码看起来像这样:
import struct
# The byte stream is received and stored in byte_stream
text = ''
i = 0
while i < len(byte_stream):
text = text + struct.unpack('c', byte_stream[i])[0]
i += 1
print(text)
但这肯定不是最有效的方式......是否有更优雅的方法来实现相同的结果?
答案 0 :(得分:1)
来自Convert bytes to a Python string:
byte_stream = [112, 52, 52]
''.join(map(chr, bytes))
>> p44