使用if ifif语句

时间:2017-05-07 00:20:47

标签: python-3.x

接口端口 - 通道1 ALLOWED_VLAN 2,4-7,27,30-31,38-39,41-42,48-50 ALLOWED_VLAN 74,678,1101-1102,1201-1202 接口端口通道2 ALLOWED_VLAN 37,51-73,75-76,1051-1052,2001 接口端口通道101 ALLOWED_VLAN 10,18-19,37,39,51-52,75-76,901-902 ALLOWED_VLAN 2901-2902,3204,3305

import re
import itertools
fileOpen3 = open('C:\\Python36\\execrice\\inter.txt')
list3 = []
for line in fileOpen3.readlines():
    if line.startswith('ALLOWED_VLAN'):
        allowedVlan = re.compile(r'\d+\S+')
        list1 = allowedVlan.findall(line)
        st1 = list1[0]
        pv1 = st1.split(',')
        list3.append(pv1)
        merged = list(itertools.chain.from_iterable(list3))
        singleVlanDigit = []
        expandedVlan1 = []
        for i in merged:
            rangeOfVlan = []
            if '-' in i:
                rangeOfVlan.append(i)
            else:
                singleVlanDigit.append(i)
                singleVlanDigit = list(map(int,singleVlanDigit))
            for j in rangeOfVlan:
                l = j.split('-')
                startVlan = int(l[0])
                endVlan = int(l[1])
                for k in range(startVlan,endVlan):
                    expandedVlan1.append(k)
        vlanallowed = singleVlanDigit + expandedVlan1
        vlanallowed = list(map(int,vlanallowed))
        print (vlanallowed)
    elif line.startswith('interface port-channel'):
        list3=[]
        print ("interface port-channel")
fileOpen3.close()

我的程序将所有数字组合在一个列表中,我希望它在读取“interface port-channel 2”时停止等等

我希望此程序的输出如下

接口端口通道 [2,27,4,5,6,30,38,41,48,49,74,678,1101,1201] 接口端口通道 [37,2001,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,75 ,1051] 接口端口通道 [10,37,39,18,51,75,901,3204,3305,2901]

但它产生以下输出

接口端口通道 [2,27,4,5,6,30,38,41,48,49] [2,27,74,678,4,5,6,30,38,41,48,49,1101,1201] 接口端口通道 [2,27,74,678,37,2001,4,5,6,30,38,41,48,49,1101,1201,51,52,53,54,55,56,57,58,59,59 ,60,61,62,63,64,65,66,67,68,69,70,71,72,75,1051] 接口端口通道 [2,27,74,678,37,2001,10,37,39,4,5,6,30,38,41,48,49,1101,1201,51,52,53,54,55,56 ,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,75,1051,18,51,75,901] [2,27,74,678,37,2001,10,37,39,3204,3305,4,5,6,30,38,41,48,49,1101,1201,51,52,53,54 ,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,75,1051,18,51,75,901,2901 ]

按照Boar的建议重新初始化elif块中的list3后,我得到了以下结果,这对我的最终结果非常重要

接口端口通道 [2,27,4,5,6,30,38,41,48,49] [2,27,74,678,4,5,6,30,38,41,48,49,1101,1201] 接口端口通道 [37,2001,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,75 ,1051] 接口端口通道 [10,37,39,18,51,75,901] [10,37,39,3204,3305,18,51,75,901,2901]

但我希望结果像这样

接口端口通道 [2,27,74,678,4,5,6,30,38,41,48,49,1101,1201] 接口端口通道 [37,2001,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,75 ,1051] 接口端口通道 [10,37,39,3204,3305,18,51,75,901,2901]

1 个答案:

答案 0 :(得分:0)

我不得不猜测数据中断线的位置,但我认为我说得对。

重复数据是因为您在错误的位置初始化list3。它应该在if line.startswith('ALLOWED_VLAN'):之后

使用该修复程序,您的程序会执行此操作:

interface port-channel
[2, 27, 4, 5, 6, 30, 38, 41, 48, 49]
[74, 678, 1101, 1201]
interface port-channel
[37, 2001, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 75, 1051]
interface port-channel
[10, 37, 39, 18, 51, 75, 901]
[3204, 3305, 2901]

接近你想要的。您只需要在打印之前组合整数列表。合并循环的vnlanallowed部分中if的值,然后打印出来并在elif部分重新初始化它。

要获得所需输出的确切格式,您需要等到打印前知道所有端口通道编号列表。您只知道当您看到新列表开始时,或者您到达文件结尾时。

import re
import itertools
fileOpen3 = open(r'E:\...\inter.txt')
channel_list = []
for line in fileOpen3.readlines():
    if line.startswith('ALLOWED_VLAN'):
        list3 = []
        allowedVlan = re.compile(r'\d+\S+')
        list1 = allowedVlan.findall(line)
        st1 = list1[0]
        pv1 = st1.split(',')
        list3.append(pv1)
        merged = list(itertools.chain.from_iterable(list3))
        singleVlanDigit = []
        expandedVlan1 = []
        for i in merged:
            rangeOfVlan = []
            if '-' in i:
                rangeOfVlan.append(i)
            else:
                singleVlanDigit.append(i)
                singleVlanDigit = list(map(int,singleVlanDigit))
            for j in rangeOfVlan:
                l = j.split('-')
                startVlan = int(l[0])
                endVlan = int(l[1])
                for k in range(startVlan,endVlan):
                    expandedVlan1.append(k)
        vlanallowed = singleVlanDigit + expandedVlan1
        vlanallowed = list(map(int,vlanallowed))
        #print (vlanallowed)
        channel_list.extend(vlanallowed)
    elif line.startswith('interface port-channel'):
        if channel_list:
            print ("interface port-channel", channel_list)
        channel_list = []
print("interface port-channel", channel_list)
fileOpen3.close()