比较python中的2个文件并打印不匹配的文本

时间:2017-05-29 08:01:47

标签: python python-2.7

我有两个文件:

Resp.txt:
vrf XXX
 address-family ipv4 unicast
  import route-target
   123:45
   212:43
  !
  export route-policy ABCDE
  export route-target
   9:43
  !
  maximum prefix 12 34
  spanning tree enable
  bandwidth 10
 !
!

和sample.txt

vrf
address-family ipv4 unicast
import route-target
export route-target
maximum prefix

我想匹配resp.txtsample.txt,这样如果resp中没有样本的内容,我会得到那些文本行。输出应该是:

spanning tree enable
bandwidth 10

我正在使用:

t2=open('sample.txt','r')
abc=open('resp.txt','r')
for x in t2:
  for line in abc:

         if x.strip() in line.strip():
          print 'yes'
         else:
          print line

但是它匹配了两个文本文件中的每一行,因此没有显示正确的结果。

1 个答案:

答案 0 :(得分:0)

因此,获取sample.txt以外的所有字符串的最简单方法是使用集合差异:

file_1 = set()
file_2 = set()

with open('Resp.txt', 'r') as f:
    for line in f:
        file_1.add(line.strip())

with open('Sample.txt', 'r') as f:
    for line in f:
        file_2.add(line.strip())

print(file_1 - file_2)

返回:

{'export route-policy ABCDE', 'vrf XXX', 'spanning tree enable', '!', '212:43', 'bandwidth 10', 'maximum prefix 12 34', '9:43', '123:45'}

但是,这不包括应用于Resp.txt的某些规则,例如:

  • 如果行是“最大前缀”,请忽略这些数字。

在阅读Resp.txt时可以应用这些规则:

import re

file_1 = set()
file_2 = set()

with open('Resp.txt', 'r') as f:
    for line in f:
        line = line.strip()
        if line == "!":
            continue
        elif re.match( r'\d+:\d+', line): # Matches times.
            continue
        elif line.startswith("vrf"):
            line = "vrf"
        elif line.startswith("maximum prefix"):
            line = "maximum prefix"
        file_1.add(line)

with open('Sample.txt', 'r') as f:
    for line in f:
        file_2.add(line.strip())

print(file_1) - file_2)

返回:

{'export route-policy ABCDE', 'bandwidth 10', 'spanning tree enable'}

这是正确的,因为sample.txt不包含route-policy

这些规则可以更加强大,但它们应该足够说明。

请注意,set只能找到唯一的差异,而不是全部(假设你有多条'spanning tree enable'行,并且想知道看到这些差异的次数。在这种情况下,你可以做更符合原始代码的内容:

import re

file_1 = []
file_2 = []

with open('Resp.txt', 'r') as f:
    for line in f:
        line = line.strip()
        if line == "!":
            continue
        elif re.match( r'\d+:\d+', line):
            continue
        elif line.startswith("vrf"):
            line = "vrf"
        elif line.startswith("maximum prefix"):
            line = "maximum prefix"
        file_1.append(line)

with open('Sample.txt', 'r') as f:
    for line in f:
        file_2.append(line.strip())

diff = []

for line in file_1:
    if line not in file_2:
        diff.append(line)

print(diff)

结果:

['export route-policy ABCDE', 'spanning tree enable', 'bandwidth 10']

虽然这种方法较慢(虽然您可能不会注意到),但它可以找到重复的行并保持找到的行的顺序。