所以基本上我有这个代码,它获取一个充满节的文本文档,并使用每个节的第一行中的指令对其进行解码,并使用它来解码每个后续行中的密码。我们的示例如下所示:
-25+122-76
?ST^jT^jLj_P^_jZQj_SPjTY[`_jQTWPx ?ST^jT^j_SPj^PNZYOjWTYPx+123+12+1234
0A:MXPBEEXA:II>GXGHPw
这是通过将第一行中的整数相加并将每个 ASCII 字符移动这么多来破译的。到目前为止,我的代码如下所示:
#Here I define the Shift function that will take a character, convert it to its ASCII numeric value, add N to it and return the ASCII character.
def Shift(char, N):
A = ord(char)
A += N
A = chr(A)
return A
#Here's the code I have that opens and reads a file's first line as instructions, evaluates the numeric value of that first line, throws rest into a list and runs the Shift helper function to eval the ASCII characters.
def driver(filename):
file = open(filename)
line = file.readline()
file = file.readlines()
N = eval(line)
codeList = list(file)
for char in codeList:
newChar = Shift(char, N)
codeList[char] = codeList[newChar]
print str(codeList)
现在我的问题是如何在节中的每个空白行之后重复我的代码?另外,如何让字符仅在 ASCII 范围 32(空格)和 126(~)内移动?这也是使用 Python 2.7.3