使用Diff作为输出的Python中的列表比较

时间:2018-07-23 07:56:25

标签: python-2.7

下面的脚本我必须比较Test1与Test2。底部提到了Test1和Test2数据。我试图使其成为通用的,以便它也适用于不同的设备。下面的脚本我必须比较Test1 vs Test2.Test1和Test2数据在底部提到。我试图使其成为通用数据,以便它也适用于不同的设备

import re
    data_cleaned = {}
    current_key = ''
    action_flag = False
    data_group = []
    if_found_vlan = True

    output = open('./output.txt','r').read()

    switch_red = re.findall(r'(\w*-RED\d{0,1})', output)[0]
    switch_blue = re.findall(r'(\w*-BLUE\d{0,1})', output)[0]

    for line in open('./output.txt'):
        m = re.match(r'(\w*-RED\d{0,1}|\w*-BLUE\d{0,1})# sh run vlan \d+', line)

        if m:
            if not if_found_vlan:
                data_cleaned[current_key].append([])
            if_found_vlan = False

            current_key = m.group(1)
            if not data_cleaned.has_key(current_key):
                data_cleaned[current_key] = []
            continue

        mm = re.match(r'vlan \d+', line)
        if mm:
            if_found_vlan = True
            action_flag = True
            data_group = []
        if action_flag and '' == line.strip():
            action_flag = False
            data_cleaned[current_key].append(data_group)

        if action_flag:
            data_group.append(line.replace('\r', '').replace('\n', ''))

    if not if_found_vlan:
        data_cleaned[current_key].append([])
    #print ("+++++++++++++++++ The missing configuration ++++++++++++++\n")
    print switch_blue + "#" + " has below missing VLAN config\n "
    p = [item for index, item in enumerate(data_cleaned[switch_blue]) if [] != [it for it in item if it not in data_cleaned[switch_red][index]]]
    print('\n'.join(['\n'.join(item) for item in p]))
    print ("+++++++++++++++++++++++++++++++\n")
    print switch_red + "#" + " has below missing VLAN config\n "
    q = [item for index, item in enumerate(data_cleaned[switch_red]) if [] != [it for it in item if it not in data_cleaned[switch_blue][index]]]
    print('\n'.join(['\n'.join(item) for item in q]))

1 个答案:

答案 0 :(得分:0)

更新原始输出,我认为使用一维列表表示您的输出不是进一步处理的好方法。

在处理数据时,我们首先需要清理数据并建立易于处理的模型以进行进一步的程序处理,因此我使用其中带有二维列表的dict对输出进行建模,最终更易于处理。

import re

data_cleaned = {}
current_key = ''
action_flag = False
data_group = []
if_found_vlan = True

for line in open('./output.txt'):
    m = re.match(r'(Test\d+)# sh run vlan \d+', line)
    if m:
        if not if_found_vlan:
            data_cleaned[current_key].append([])
        if_found_vlan = False

        current_key = m.group(1)
        if not data_cleaned.has_key(current_key):
            data_cleaned[current_key] = []
        continue

    mm = re.match(r'vlan \d+', line)
    if mm:
        if_found_vlan = True
        action_flag = True
        data_group = []
    if action_flag and '' == line.strip():
        action_flag = False
        data_cleaned[current_key].append(data_group)

    if action_flag:
        data_group.append(line.replace('\r', '').replace('\n', ''))

if not if_found_vlan:
    data_cleaned[current_key].append([])

print ("+++++++++++++++++ The missing configuration is++++++++++++++\n")
p = [item for index, item in enumerate(data_cleaned['Test2']) if [] != [it for it in item if it not in data_cleaned['Test1'][index]]]
print('\n'.join(['\n'.join(item) for item in p]))
print ("+++++++++++++++++ The missing configuration is++++++++++++++\n")
q = [item for index, item in enumerate(data_cleaned['Test1']) if [] != [it for it in item if it not in data_cleaned['Test2'][index]]]
print('\n'.join(['\n'.join(item) for item in q]))