0
votes

I have large files contain cipher texts. the ciphers are connected like this

QJFQFJDOVRVLHSVUJNHTZDGLDOVGSAYFKFPSAJSOEGXCDHCDDEWGTUUFOVDGQUJD

my question is how can I split the letters so I can for loop each letter?

I'm trying this code but I get this error message

num = (ord(symbol)-k) % 126

TypeError: ord() expected string of length 1, but list found"

with open(file) as f:
    translated = ''
    for s in f:
            symbol = s.split()
            print(symbol)
            num = (ord(symbol)-k) % 126

            if num < 32:
                    num += 95
            translated += chr(num)
    print(translated)

before adding .split() function I got this error message:

"TypeError: ord() expected a character, but string of length 6 found"

with this code:

with open(file) as f:
    translated = ''
    for symbol in f:
            print(symbol)
            num = (ord(symbol)-k) % 126

            if num < 32:
                    num += 95
            translated += chr(num)
    print(translated)

My ciphert contains no white spaces

Thank you

1
s is the line in the file . . . you'll want to iterate over that, and python lets you iterate over strings directly (i.e. for _ in s)ernie

1 Answers

2
votes

There is nothing in your file to split (as there are no spaces or any other separators). If your file is not ridiculously large, read its content as a string and then process the string as a collection of characters:

data = open(file).read()
for char in data:
     #do something with your char, like ord(char)

If the file is large, read it piecewise with .read(n), and you will still get strings to work with.