所以基本上我有這樣的代碼,它需要一個文本文檔充滿了節並使用每節的第一行中的指令解碼它們,並使用它來解碼每個後續的密碼線。 我們的示例是這樣的:?在文件(Python)中的空白行之後重申一個循環
-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
您應該非常小心'eval'函數。如果用戶提供的文件的第一行讀取「os.system(」rm -rf /「)」? – 2013-03-13 17:07:03
這不在我們正在處理的參數範圍內。我們給出了一組特定的指令來爲該項目的每個部分定義,這部分僅涉及+/-說明。 –
2013-03-13 17:56:46