简体   繁体   English

如何将列表转换为ASCII

[英]How to convert a list into ASCII

How can I convert a list into ASCII, but I want to have a list again after I converted it. 如何将列表转换为ASCII,但我希望在转换后再次列出一个列表。

I found this for converting from ASCII to a list: 我发现这是为了从ASCII转换为列表:

L = [104, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100]
print(''.join(map(chr,L)))

But it doesn't work reversed. 但它不起作用。

This is my input: 这是我的意见:

L = ['h','e','l','l','o']

And I want this as output: 我希望这是输出:

L = ['104','101','108','108','111']

This is a much simpler solution: 这是一个更简单的解决方案:

L = ['h','e','l','l','o']
changer = [ord(x) for x in L]
print(changer)

Using the function ord() 使用函数ord()

This worked well for me. 这对我很有用。

>>> L = [104, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100]
>>> print [ chr(x) for x in L]
['h', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd']
L = ['h','e','l','l','o']

print(list(map(ord,L)))
#output : [104, 101, 108, 108, 111]

print(list(map(str,map(ord,L))))
#output : ['104', '101', '108', '108', '111']
L = ['h','e','l','l','o']
print(list(map(str, map(ord, L))))

This outputs: 这输出:

['104', '101', '108', '108', '111']

This works the other way round: 这相反:

L = ['h', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd']
L = [ord(x) for x in L]
print(L)

output: 输出:

[104, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100]

From here 这里开始

You can try: 你可以试试:

L = ['h','e','l','l','o']
for x in range(len(L)):
     L[x] = ord(L[x])
print(L)

Ouput: 输出继电器:

[104,101,108,108,111]

EDIT: 编辑:

ord() allows you to convert from char to ASCII whereas chr() allows you to do the opposite ord()允许您从char转换为ASCII,而chr()允许您执行相反的操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM