dimanche 16 octobre 2016

Find word in string of letters Python

After commenting sections out and testing each section with a print, I can tell that my Columnar Transposition is working, except when I search the string of letters which is "cipher" for "stripcommon" it never seems to find any as the counter never goes up.
I believe there may be a problem because when I print out the cipher and common word, they seem to have spaces even though I dont have any. I have tried to strip of all spaces but this does not seem to work either. Maybe I am just searching for this the wrong way as well.

My problem seems to occur somewhere in the 3rd for loop, as it does print, but does not increase the counter.

My code for reference:

#/usr/bin/python

from pycipher import ColTrans
f1 = open('ciphers.txt', 'r')
f4 = open('output.txt', 'w')
count = 0

for line in f1:
    spaceline = line.rstrip()
    stripline = spaceline.rstrip(' ')
    f2 = open('passwordList.txt', 'r')
    for key in f2:
        spacekey = key.rstrip()
        stripkey = spacekey.rstrip(' ')
        if len(stripkey) < 11 and len(stripkey) > 1:
            cipher = ColTrans(stripkey).decipher(stripline)
            f3 = open('common.txt', 'r')
            for common in f3:
                spacecommon = common.rstrip(' ')
                stripcommon = spacecommon.rstrip()
                if len(stripcommon) > 1:
                    print('Test Counter,cipher,key,commonword',count,cipher,stripkey,stripcommon)
                    if stripcommon in cipher:
                        count = count + 1
                        print('Found ', stripcommon, ' in ', cipher)
                    if count > 15:
                        print('Word count: ', count, '     ', cipher)
                        f4.write(cipher, '\n')
            f3.close()
        count = 0
    f2.close()
f1.close()
f4.close()

Aucun commentaire:

Enregistrer un commentaire