怎么把Ascii列表转换成文本?

时间:2018-12-14 21:58:37

标签: python list text ascii

aList = [[ord(ch) for ch in word] for word in wordlist]
bList = [[x - offset for x in word] for word in aList]
cList = [[x + 94 for x in word if x < 33] for word in bList]
print(cList)
for i in cList:
    text = chr(i)
    print(text)

这是我将文本转换为正确的ASCII值后用来加密和解密文本的程序的结尾。我不知道如何在没有错误的情况下将该值转换为文本:

TypeError: an integer is required (got type list)

3 个答案:

答案 0 :(得分:0)

str函数是一个旨在将任何内容转换为字符串的函数。如果您有一个名为mylist的列表,则可以将其转换为这样的字符串:

str(mylist)

此时,您可以使用它来做任何您想做的事情。有关str的更多信息,请参见here

答案 1 :(得分:0)

cList中的每个元素本身就是一个python列表,因此您不能将其传递给chr。相反,您还需要遍历cList中每个列表中的每个元素:

aList = [[ord(ch) for ch in word] for word in wordlist]
bList = [[x - ofset for x in word] for word in aList]
cList = [[x + 94 for x in word if x < 33] for word in bList]
print(cList)
for arr in cList:
    for i in arr:
        text = chr(i)
        print(text)

答案 2 :(得分:0)

您从单词列表(List)开始。每个单词都是一个字符串。然后,您将每个单词拆分为单个字符,将它们转换为整数,然后进行一些转换(bListcList)。至此,您将获得一个字符列表。

如果您的目标是将每个嵌套列表转换回字符串,则最简单的方法可能是使用str.join

dList = [str.join(chr(c) for c in row) for row in cList]
for word in dList:
    print(word)
相关问题