python

时间:2016-04-21 03:57:06

标签: python decoding

嘿伙计们,我的程序有问题,执行以下操作:

1。)接收一个文件,该文件生成将被假定为平均值的字母的相对频率。

2.)获取包含编码消息的第二个文件。

3.)测试每个可能的旋转。

4。)创建一个新的txt文件,其中包含解码的消息作为输出

这是我的代码:

# This is the module that we import to check if a file name exists
import os 


# This is the dictionary used later to store individual letter counts, which
# allows us to calculate the relative frequency of each letter
d1 = { }
d1['a'] = 0
d1['b'] = 0
d1['c'] = 0
d1['d'] = 0
d1['e'] = 0
d1['f'] = 0
d1['g'] = 0
d1['h'] = 0
d1['i'] = 0
d1['j'] = 0
d1['k'] = 0
d1['l'] = 0
d1['m'] = 0
d1['n'] = 0
d1['o'] = 0
d1['p'] = 0
d1['q'] = 0
d1['r'] = 0
d1['s'] = 0
d1['t'] = 0
d1['u'] = 0
d1['v'] = 0
d1['w'] = 0
d1['x'] = 0
d1['y'] = 0
d1['z'] = 0


# This asks for the user to enter a file to parse 
filename = raw_input("Path to a file to parse:  ")


# This is the basic if/else statement that keeps track of each letter counter 
# in the dictionary above if the file exists, and displays and error message
# and quits if it doesn't exist.
if os.path.exists(filename):
    f = open(filename, 'r')
    counter = 0
    for line in f:
        for j in line:
            if j.isalpha():
                counter += 1
                d1[j.lower()] += 1
    f.close()
else:
    print "Error: cannot find",filename
    quit()


# This is the definition that give us the relative frequency by dividing the
# dictionary key value for each character by the total number of characters
def relfreq(character):
    return d1[character] / float(counter)


### This is the end of the previous module's code ###

# This code creates a list of the average frequencies of letter 
lof1 = [relfreq('a'), relfreq('b'), relfreq('c'), relfreq('d'), relfreq('e'), 
       relfreq('f'), relfreq('g'), relfreq('h'), relfreq('i'), relfreq('j'), 
       relfreq('k'), relfreq('l'), relfreq('m'), relfreq('n'), relfreq('o'),
       relfreq('p'), relfreq('q'), relfreq('r'), relfreq('s'), relfreq('t'), 
       relfreq('u'), relfreq('v'), relfreq('w'), relfreq('x'), relfreq('y'), 
       relfreq('z')]

# This code finds the relative frequency of the coded message
d2 = { }
d2['a'] = 0
d2['b'] = 0
d2['c'] = 0
d2['d'] = 0
d2['e'] = 0
d2['f'] = 0
d2['g'] = 0
d2['h'] = 0
d2['i'] = 0
d2['j'] = 0
d2['k'] = 0
d2['l'] = 0
d2['m'] = 0
d2['n'] = 0
d2['o'] = 0
d2['p'] = 0
d2['q'] = 0
d2['r'] = 0
d2['s'] = 0
d2['t'] = 0
d2['u'] = 0
d2['v'] = 0
d2['w'] = 0
d2['x'] = 0
d2['y'] = 0
d2['z'] = 0

filename2 = raw_input("Path to encoded message:  ")

if os.path.exists(filename2):
    f2 = open(filename2, 'r')
    counter2 = 0
    for line2 in f2:
        for j2 in line2:
            if j2.isalpha():
                counter2 += 1
                d2[j2.lower()] += 1
    f2.close()
else:
    print "Error: cannot find",filename2
    quit()

def relfreq2(character):
    return d2[character] / float(counter2)

# This code creates a list of relative frequencies of the coded message
lof2 = [relfreq2('a'), relfreq2('b'), relfreq2('c'), relfreq2('d'), relfreq2('e'), 
       relfreq2('f'), relfreq2('g'), relfreq2('h'), relfreq2('i'), relfreq2('j'), 
       relfreq2('k'), relfreq2('l'), relfreq2('m'), relfreq2('n'), relfreq2('o'),
       relfreq2('p'), relfreq2('q'), relfreq2('r'), relfreq2('s'), relfreq2('t'), 
       relfreq2('u'), relfreq2('v'), relfreq2('w'), relfreq2('x'), relfreq2('y'), 
       relfreq2('z')]










##### Not sure if this is correct #####
scores = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

d3 = { }
d3['a'] = 0
d3['b'] = 1
d3['c'] = 2
d3['d'] = 3
d3['e'] = 4
d3['f'] = 5
d3['g'] = 6
d3['h'] = 7
d3['i'] = 8
d3['j'] = 9
d3['k'] = 10
d3['l'] = 11
d3['m'] = 12
d3['n'] = 13
d3['o'] = 14
d3['p'] = 15
d3['q'] = 16
d3['r'] = 17
d3['s'] = 18
d3['t'] = 19
d3['u'] = 20
d3['v'] = 21
d3['w'] = 22
d3['x'] = 23
d3['y'] = 24
d3['z'] = 25

def get_scores():
    ii = 0
    jj = 0
    for ii in range(25):      
        for jj in range(26):
            if ii + jj <26: 
                scores[jj] += lof1[jj] * lof2[jj + ii]  
                jj += 1 
            else: 
               scores[jj] += lof1[jj] * lof2[jj + ii - 26]  
               jj += 1  
        ii += 1    

# This is the code that determines which match is the best match
get_scores()
rotationscore = max(scores)
rotations_ttr = scores.index(rotationscore)

print "Shift",rotations_ttr,"letters to the right"





loa = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r',
       's','t','u','v','w','x','y','']
# This code 'decodes' the coded message 
if os.path.exists(filename):
    f3 = open(filename2, 'r')
    counter3 = 0
    for line3 in f3:
        for j3 in line3:
            if j2.isalpha():
                counter3 += 1
                j3 = d3[j3.lower()]
                line3.replace(loa[int(j3)], loa[int(j3 + rotations_ttr)])
    print 
    f.close()

我目前收到错误:

  

要解析的文件的路径:./ Pepenix.py Traceback(最近一次调用   最后):文件&#34; / Users / atloftus / Desktop / Lecture Code / Labs / decipher.py&#34;,   第85行,在       lof1 = [relfreq(&#39; a&#39;),relfreq(&#39; b&#39;),relfreq(&#39; c&#39;),relfreq(&#39; d&#39;) ,relfreq(&#39; e&#39;),文件&#34; / Users / atloftus / Desktop / Lecture   代码/实验室/ decipher.py&#34;,第79行,在relfreq中       return d1 [character] / float(counter)ZeroDivisionError:float division by zero

如何摆脱这个错误?它不是早些时候,现在我不知道我改变了什么导致它。感谢

0 个答案:

没有答案