如何在Python中将ASCII值列表转换为字符串?
我在一个Python程序里有一个数字列表,这些数字是ASCII值。请问我该怎么把这些ASCII值转换成一个“普通”的字符串,然后显示在屏幕上呢?
9 个回答
14
import array
def f7(list):
return array.array('B', list).tostring()
28
这个解决方案跟其他人说的差不多,但我个人更喜欢用map函数,而不是列表推导式:
>>> L = [104, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100]
>>> ''.join(map(chr,L))
'hello, world'
164
你可能在找'chr()'这个函数:
>>> L = [104, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100]
>>> ''.join(chr(i) for i in L)
'hello, world'