Python 2.7 Python中以Diff作为输出的列表比较

Python 2.7 Python中以Diff作为输出的列表比较,python-2.7,Python 2.7,下面的脚本我必须比较Test1与Test2.Test1和Test2数据,在底部提到。我试图使其成为一个通用脚本,以便它也适用于不同的设备。下面的脚本我必须比较Test1与Test2.Test1和Test2数据,在底部提到。我试图使其成为一个通用脚本,以便它适用于不同的设备设备也 import re data_cleaned = {} current_key = '' action_flag = False data_group = [] if_found_

下面的脚本我必须比较Test1与Test2.Test1和Test2数据,在底部提到。我试图使其成为一个通用脚本,以便它也适用于不同的设备。下面的脚本我必须比较Test1与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]))

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

当我们处理一个数据时,我们首先需要清理数据并设置一个易于处理的模型,以便进行进一步的程序处理,因此我使用一个
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]))