繁体   English   中英

在txt文件中搜索python

[英]search in txt file python

我已经在这个代码上工作了几天,但没有结果。 该函数autodecrypt采用由encyrpted串encrypt其字符域中AZ和AZ通过许多改变offset 所以在ASCII代码中,'A'将是65,如果offset = 7,那么'A'现在是'H'(其代码#是72)。 将尝试从0-95的偏移值。 如果85%或更多的单词出现在名为dictionary.txt的txt文件中,则会检查解密,该文件基本上包含一堆常用单词。 这就是我的问题所在:如果我生成的字符串在txt文件中,则无法正确检查。

def autodecrypt(ciphertext):
    text = list(ciphertext)
    t = open ('dictionary.txt', 'r')
    m = t.read()
    diccond = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ' #initializing variables
    ntext = []
    ctext = ''
    i = 0
    offset = 0
    check = 0
    while offset <= 95:                   #cycling through 95 offset values
        while i < len(text):              #decrypting ciphertext with given offset value
            r = ord(text[i])
            if r - offset < 32:
                s = chr(127 - (offset - (r - 32)))
            else:
                s = chr(r - offset)
            ntext.append(s)
            i+=1
        stext = ''.join(ntext) #make decrypted list (ntext) into string, stext
        for j in stext:       #removing punctuation and store in new string, ctext
            if j in diccond:
                ctext += j
        cltext = ctext.lower() #lowercasing ctext string
        for k in (cltext.split(' ')):  #checking if ctext, list version, is in txt file
            if k in m:
                check +=1
        if check / len(ctext.split(' ')) >= 0.85: #checking if 85% or over of ctext string version in txt file
            return stext
        else:                            #else if there is not any fail and return cipher text (see below)
            fail = 0
        check = 0 #resetting all variables 
        ctext = ''
        ntext=[]  
        i=0
        offset +=1 #increasing offset
    if fail == 0:
        return ciphertext

此外,如果没有85%或更多匹配,则返回原始加密字符串。

假设这是Python 2.x(你需要更具体地标记!),问题似乎在这里: check / len(ctext.split(' '))这是一个整数除法,0和1是唯一可能的结果(如果文件中的每个单词都只能为1)。 要获得浮点结果,您需要在除法之前将至少一个操作数转换为float: float(check) / len(ctext.split(' '))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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