So basically I have this code that takes a text document full of stanzas and decodes them using instructions in the first line of each stanza and uses it to decode the cipher in each subsequent line. Our sample looks like this:
-25+122-76
?ST^jT^jLj_P^_jZQj_SPjTY[`_jQTWPx ?ST^jT^j_SPj^PNZYOjWTYPx+123+12+1234
0A:MXPBEEXA:II>GXGHPw
This is deciphered by adding the integers in the first line and shifting each ASCII character by that much. My code so far looks like this:
#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)
Now my question is how can I have my code reiterate after every blank line in the stanza? Also how do I get the characters to only shift within ASCII range 32(space) and 126(~)? Also this is using Python 2.7.3