简体   繁体   中英

Reiterating a loop after blank lines in file (Python)

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

For keeping it inside the range, you can utiltise a deque and I'd also make eval go bye-bye and manually convert the numbers to ints first, and then use a translation table to decode the data, eg:

data = """-25+122-76
?ST^jT^jLj_P^_jZQj_SPjTY[`_jQTWPx ?ST^jT^j_SPj^PNZYOjWTYPx"""

lines = data.splitlines()

import re
from collections import deque
from string import maketrans

# Insted of using `eval` - find number with signs and sum
shift = sum(int(i) for i in re.findall('[-+]\d+', lines[0]))
# Explicit range of characters
base_set = map(chr, range(32, 127))
# Create a new deque which can be rotated and rotate by shift
d = deque(base_set)
d.rotate(-shift)
# Make translation table and translate
t = maketrans(''.join(base_set), ''.join(d))
print lines[1].translate(t)
# This is a test of the input file.5This is the second line.
file = open(filename)
while True:
    line = file.readline()
    if not line:      # if end of file, exit
        print "Reached end of file"
        break
    if line == "\n":  # if new line, or empty line, continue
        continue
    else:
        your function

As far as keeping everything in ASCII range, I'll have to get back to you on that, if not quick answer, try another control structure to keep everything in the correct range, simple math should do.

You can also refer to this: link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM