将元组中的字节转换为字符串

时间:2018-09-05 20:17:48

标签: python python-3.x

我有一个充满字节的元组,我需要将这些字节转换为单个utf-8字符串,我该怎么做?

data = tuple([0, 4, 4, 6, 1, 53])
print(data as utf-8 string?)

1 个答案:

答案 0 :(得分:2)

data = bytes(tuple([0, 4, 4, 6, 1, 53])).decode('utf-8')

输出:

'\x00\x04\x04\x06\x015'

使用的功能:

bytes() - convert an iterable to a bytes like object
decode('utf-8') - decode bytes to utf8