如何将ascii代码转换为文本?

时间:2016-10-02 05:03:56

标签: python ascii

我需要能够获取输入,删除特殊字符,将所有大写字母设为小写,转换为偏移量为5的ascii代码,然后将这些偏移值转换回字符以形成单词。我一直得到TypeError:'int'对象最后不能迭代

string=(str(raw_input("The code is:")))

#change it to lower case 
string_lowercase=string.lower()

print "lower case string is:", string_lowercase

#strip special characters

specialcharacters="1234567890~`!@#$%^&*()_-+={[}]|\:;'<,>.?/"

for char in specialcharacters:
    string_lowercase=string_lowercase.replace(char,"")

print "With the specials stripped out the string is:", string_lowercase

#input offset

offset=(int(raw_input("enter offset:")))

#converstion of text to ASCII code 

for i in string_lowercase:
    code=ord(i)
    result=code-offset

#converstion from ASCII code to text
for number in result:
    message=repre(unichr(number))
    print message

1 个答案:

答案 0 :(得分:0)

您将获得TypeError: 'int' object is not iterable at the end,因为您每次在循环中声明result为int。 result应该是一个列表,并且每个代码都通过循环附加到它上面,如下所示:

#converstion of text to ASCII code 
result = []
for i in string_lowercase:
    code=ord(i)
    result.append(code-offset)