Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何比较python中不同文件的值_Python_File_Networking - Fatal编程技术网

如何比较python中不同文件的值

如何比较python中不同文件的值,python,file,networking,Python,File,Networking,我对Python非常陌生,对处理文件非常缺乏经验。这就是为什么,我面临着一个巨大的问题,使一个简单的程序。我的程序中输入了两个文件: file1.txt: access-list 1 deny 172.16.4.13 0.0.0.0 access-list 1 permit 172.16.5.0 0.0.0.255 interface EO ip access-group 1 out 172.16.4.13 172.16.3.2 172.16.5.2 172.16.3.4 172.16.5.

我对Python非常陌生,对处理文件非常缺乏经验。这就是为什么,我面临着一个巨大的问题,使一个简单的程序。我的程序中输入了两个文件:

file1.txt:

access-list 1 deny 172.16.4.13 0.0.0.0
access-list 1 permit 172.16.5.0 0.0.0.255

interface EO
ip access-group 1 out
172.16.4.13 172.16.3.2
172.16.5.2 172.16.3.4
172.16.5.0 172.16.3.4
file2.txt:

access-list 1 deny 172.16.4.13 0.0.0.0
access-list 1 permit 172.16.5.0 0.0.0.255

interface EO
ip access-group 1 out
172.16.4.13 172.16.3.2
172.16.5.2 172.16.3.4
172.16.5.0 172.16.3.4
输出将如下所示:

172.16.4.13 172.16.3.2 denied
172.16.5.0 172.16.3.4 permit
我的逻辑是:

  • file1.txtatpermission变量保存deny/permission单词

  • ip变量处保存file1.txt中的第一个ip地址

  • 分别将文件2.txt中的两个ip地址保存在目标变量中

  • ip变量与源变量进行比较,如果它们相等,则将行打印为
    ip/source,destination permit/deny

  • 为此,我按照以下方式安排了代码:

    file1 = open("file1.txt","r")
    
    line = file1.readline()
    while line:
        values = line.split()
        permission = values[2]
        ip = values[3]
        line = file1.readline()
    
    
    
    file2 = open("file2.txt","r")
    
    line = file2.readline()
    while line:
        value = line.split()
        source = value[0]
        destination = value[1]
        if source == ip:
            print(ip, permission, destination)  
        line = file2.readline()
    
    file2.close()
    
    file1.close()
    

    但是,这不能正常工作:(我怎样才能编写这个简单的程序?

    试试这样的方法

    with open('file1.txt') as f1:
        fd1 = {}
        for ln in f1:
            if ln.startswith('access-list'):
                vals = ln.split()
                fd1[vals[3]] = vals[2]
    
    with open('file2.txt') as f2:
        for ln in f2:
            vals = ln.split()
            if vals[0] in fd1:
                print vals[0], fd1[vals[0]], vals[1]
    
    您最初的问题的一部分是您没有存储文件1中的信息-您只查看了最后一项
    请记住,这只适用于精确匹配,而不适用于我在上面的评论中提到的范围

    您的缩进有一点是错误的,但这种方法不会真正满足您的需要您忽略了该内容的掩码部分-在您的示例访问列表1中,允许172.16.5.0.0.0.255也允许172.16.5.2 172.16.3.4-您忽略了这一点这一行给了我一个错误:vals=ln.split():!哎哟,我有一个问题:你说“fd1”是什么意思?它只是一个字典,用来将第一个文件存储在我的文件字典里