簡體   English   中英

如何檢查下一個iter項而不進行Python中的For循環

[英]How to check next iter item without proceeding in For loop in Python

當通過next(iter)迭代時我想基本上將網絡組合在一起,我通過計算每行中出現的項目數來做到這一點,如果它有7個項目,則它是條目的開始。

然后我在輸入行下方查找行,直到找到包含7個項目的另一行。 然后這個過程重新開始。

唯一的問題是當我通過line = next(bgp_table_iter)訪問“下一行”並打破循環時,如果“下一行”有7個項目意味着開始一個新條目,當它開始返回for line in bgp_table_iter:它再次迭代到下一行,跳過應該有7個項目的原始“下一行”並且是新條目的開始。

哪一端跳過其他所有條目。

我怎么能阻止跳過? 或者讓For循環觸發器在中斷時轉到上一次迭代?

def show_ip_bgp():
    data = test_output_short.split('RPKI validation codes: V valid, I invalid, N Not found\n\n     Network          Next Hop            Metric LocPrf Weight Path\n')
    bgp_table = data[1]
    bgp_table = re.sub("     Network          Next Hop            Metric LocPrf Weight Path\n","",bgp_table)
    bgp_table = re.sub("\*","",bgp_table)

    bgp_table_list = bgp_table.split('\n')

    bgp_table_iter = iter(bgp_table_list)

    overall_dict = {}
    index = 0
    for line in bgp_table_iter:
        # print line.split(), len(line.split())

        current_line = line.split()
        # If 7 items are in the list it's the start of a network
        if len(current_line) == 7:
            best = True if '>' in current_line[0] else False
            network = current_line[1]
            attached_ip = current_line[2]
            print "Found Starting Network", network, attached_ip, "Best Path: {}".format(best)
            for i in range(0,1000):
                line = next(bgp_table_iter)
                subline_line = line.split()
                best_sub = 'True' if '>' in subline_line[0] else 'False'
                if len(subline_line) == 6:
                    print 'Sub:', subline_line[1], "Best Path: {}".format(best_sub)
                elif len(subline_line) == 5:
                    print 'Sub:', subline_line[1], "Best Path: {}".format(best_sub)
                elif len(subline_line) == 7:
                    break



test_output_short = '''BGP table version is 3538854, local router ID is 10.15.1.81
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
              r RIB-failure, S Stale, m multipath, b backup-path, f RT-Filter,
              x best-external, a additional-path, c RIB-compressed,
Origin codes: i - IGP, e - EGP, ? - incomplete
RPKI validation codes: V valid, I invalid, N Not found

     Network          Next Hop            Metric LocPrf Weight Path
 * i 10.0.1.0/24      10.8.111.45              0    115      0 i
 *>i                  10.8.11.45               0    120      0 i
 * i                  10.8.11.45               0    120      0 i
 * i                  10.8.11.45               0    120      0 i
 * i 10.0.3.0/29      10.8.111.45              0    115      0 i
 *>i                  10.8.11.45               0    120      0 i
 * i                  10.8.11.45               0    120      0 i
 * i                  10.8.11.45               0    120      0 i
 * i 10.8.0.0/16      10.9.0.1                 0     50      0 i
 * i                  10.8.0.1                 0     50      0 i
 *>                   0.0.0.0                  0         32768 i
 * i 10.9.0.0/16      10.9.0.1                 0     50      0 i
 * i                  10.8.0.1                 0     50      0 i
 *>                   0.0.0.0                  0         32768 i
 *>i 10.10.2.0/24     10.8.10.2                0    100      0 i
 * i                  10.8.10.2                0    100      0 i
 * i                  10.8.10.2                0    100      0 i
 * i 10.10.5.0/24     10.8.142.15              0     85      0 i
 *>i                  10.8.42.15               0    100      0 i
 * i                  10.8.42.15               0    100      0 i
 * i                  10.8.42.15               0    100      0 i
 *>i 10.10.7.0/24     10.8.40.84               0    100      0 i
 * i                  10.8.40.84               0    100      0 i
 * i                  10.8.40.84               0    100      0 i
 *>i 10.10.8.0/24     10.8.10.8                0    100      0 i
 * i                  10.8.110.8               0     85      0 i
 * i                  10.8.10.8                0    100      0 i
 * i                  10.8.10.8                0    100      0 i
 *>i 10.10.11.0/24    10.8.42.8                0    100      0 i
 * i                  10.8.42.8                0    100      0 i
 * i                  10.8.42.8                0    100      0 i
 * i                  10.9.42.8                0    100      0 i
 * i 10.10.12.0/24    10.8.10.12               0    100      0 i
 * i                  10.8.10.12               0    100      0 i
 *>i                  10.8.10.12               0    100      0 i'''

輸出:

>>> show_ip_bgp()
Found Starting Network 10.0.1.0/24 10.8.111.45 Best Path: False
Sub: 10.8.11.45 Best Path: True
Sub: 10.8.11.45 Best Path: False
Sub: 10.8.11.45 Best Path: False
Found Starting Network 10.8.0.0/16 10.9.0.1 Best Path: False
Sub: 10.8.0.1 Best Path: False
Sub: 0.0.0.0 Best Path: True
Found Starting Network 10.10.2.0/24 10.8.10.2 Best Path: True
Sub: 10.8.10.2 Best Path: False
Sub: 10.8.10.2 Best Path: False
Found Starting Network 10.10.7.0/24 10.8.40.84 Best Path: True
Sub: 10.8.40.84 Best Path: False
Sub: 10.8.40.84 Best Path: False
Found Starting Network 10.10.11.0/24 10.8.42.8 Best Path: True
Sub: 10.8.42.8 Best Path: False
Sub: 10.8.42.8 Best Path: False
Sub: 10.9.42.8 Best Path: False

更新:

在我運行測試之前,建議的修復程序似乎有效。 它確實使所有網絡看起來都像。

def show_ip_bgp():data = test_output_very_short.split('RPKI驗證碼:V有效,I無效,N未找到\\ n \\ n網絡下一跳度量標准LocPrf權重路徑\\ n')bgp_table = data [1] bgp_table = re。 sub(“網絡下一跳度量標准LocPrf權重路徑\\ n”,“”,bgp_table)bgp_table = re.sub(“*”,“”,bgp_table)

bgp_table_list = bgp_table.split('\n')

bgp_table_iter = iter(bgp_table_list)

overall_dict = {}
index = 0
for i, line in enumerate(bgp_table_list):
    # print line.split(), len(line.split())

    current_line = line.split()
    # If 7 items are in the list it's the start of a network
    if len(current_line) == 7:
        best = True if '>' in current_line[0] else False
        network = current_line[1]
        attached_ip = current_line[2]
        print "Found Starting Network", network, attached_ip, "Best Path: {}".format(best)
        for i in range(0,1000):
            line = bgp_table_list[i+1]
            print i
            subline_line = line.split()
            best_sub = 'True' if '>' in subline_line[0] else 'False'
            if len(subline_line) == 6:
                print 'Sub:', subline_line[1], "Best Path: {}".format(best_sub)
            elif len(subline_line) == 5:
                print 'Sub:', subline_line[1], "Best Path: {}".format(best_sub)
            elif len(subline_line) == 7:
                line = bgp_table_list[i+1]
                break

輸出:

Found Entry Network:  10.0.1.0/24 Nexthop: 10.8.111.45 Best Path: False Pref: 115
0
Found Network:  10.0.1.0/24 Nexthop: 10.8.11.45 Best Path: True Pref: 120
1
Found Network:  10.0.1.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
2
Found Network:  10.0.1.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
3
Found Entry Network:  10.0.3.0/29 Nexthop: 10.8.111.45 Best Path: False Pref: 115
0
Found Network:  10.0.3.0/29 Nexthop: 10.8.11.45 Best Path: True Pref: 120
1
Found Network:  10.0.3.0/29 Nexthop: 10.8.11.45 Best Path: False Pref: 120
2
Found Network:  10.0.3.0/29 Nexthop: 10.8.11.45 Best Path: False Pref: 120
3
Found Entry Network:  10.8.0.0/16 Nexthop: 10.9.0.1 Best Path: False Pref: 50
0
Found Network:  10.8.0.0/16 Nexthop: 10.8.11.45 Best Path: True Pref: 120
1
Found Network:  10.8.0.0/16 Nexthop: 10.8.11.45 Best Path: False Pref: 120
2
Found Network:  10.8.0.0/16 Nexthop: 10.8.11.45 Best Path: False Pref: 120
3
Found Entry Network:  10.9.0.0/16 Nexthop: 10.9.0.1 Best Path: False Pref: 50
0
Found Network:  10.9.0.0/16 Nexthop: 10.8.11.45 Best Path: True Pref: 120
1
Found Network:  10.9.0.0/16 Nexthop: 10.8.11.45 Best Path: False Pref: 120
2
Found Network:  10.9.0.0/16 Nexthop: 10.8.11.45 Best Path: False Pref: 120
3
Found Entry Network:  10.10.2.0/24 Nexthop: 10.8.10.2 Best Path: True Pref: 100
0
Found Network:  10.10.2.0/24 Nexthop: 10.8.11.45 Best Path: True Pref: 120
1
Found Network:  10.10.2.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
2
Found Network:  10.10.2.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
3
Found Entry Network:  10.10.5.0/24 Nexthop: 10.8.142.15 Best Path: False Pref: 85
0
Found Network:  10.10.5.0/24 Nexthop: 10.8.11.45 Best Path: True Pref: 120
1
Found Network:  10.10.5.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
2
Found Network:  10.10.5.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
3
Found Entry Network:  10.10.7.0/24 Nexthop: 10.8.40.84 Best Path: True Pref: 100
0
Found Network:  10.10.7.0/24 Nexthop: 10.8.11.45 Best Path: True Pref: 120
1
Found Network:  10.10.7.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
2
Found Network:  10.10.7.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
3
Found Entry Network:  10.10.8.0/24 Nexthop: 10.8.10.8 Best Path: True Pref: 100
0
Found Network:  10.10.8.0/24 Nexthop: 10.8.11.45 Best Path: True Pref: 120
1
Found Network:  10.10.8.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
2
Found Network:  10.10.8.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
3
Found Entry Network:  10.10.11.0/24 Nexthop: 10.8.42.8 Best Path: True Pref: 100
0
Found Network:  10.10.11.0/24 Nexthop: 10.8.11.45 Best Path: True Pref: 120
1
Found Network:  10.10.11.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
2
Found Network:  10.10.11.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
3
Found Entry Network:  10.10.12.0/24 Nexthop: 10.8.10.12 Best Path: False Pref: 100
0
Found Network:  10.10.12.0/24 Nexthop: 10.8.11.45 Best Path: True Pref: 120
1
Found Network:  10.10.12.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
2
Found Network:  10.10.12.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
3
Found Entry Network:  10.10.15.0/24 Nexthop: 10.8.10.15 Best Path: False Pref: 100
0
Found Network:  10.10.15.0/24 Nexthop: 10.8.11.45 Best Path: True Pref: 120
1
Found Network:  10.10.15.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
2
Found Network:  10.10.15.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
3
Found Entry Network:  10.10.27.0/24 Nexthop: 10.8.41.81 Best Path: False Pref: 100
0
Found Network:  10.10.27.0/24 Nexthop: 10.8.11.45 Best Path: True Pref: 120
1
Found Network:  10.10.27.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
2
Found Network:  10.10.27.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
3

索引號回到0,每次顯示這里打印出被檢查行的索引。

不要強迫它成為一個iter ,而是繼續使用它作為list ,然后使用enumerate來知道list的位置。

編輯:Chaning首先枚舉使用index ,第二個循環的范圍以1而不是0 ,行檢查到line = bgp_table_list[index+i] ,當它到達文件末尾時帶有except子句。

for index, line in enumerate(bgp_table_list):
    # print line.split(), len(line.split())

    current_line = line.split()
    # If 7 items are in the list it's the start of a network
    if len(current_line) == 7:
        best = True if '>' in current_line[0] else False
        network = current_line[1]
        attached_ip = current_line[2]
        print "Found Starting Network", network, attached_ip, "Best Path: {}".format(best)
        for i in range(1, 1000):
            try:
                line = bgp_table_list[index+i]
            except IndexError:
                break
            subline_line = line.split()
            best_sub = 'True' if '>' in subline_line[0] else 'False'
            if len(subline_line) == 6:
                print 'Sub:', subline_line[1], "Best Path: {}".format(best_sub)
            elif len(subline_line) == 5:
                print 'Sub:', subline_line[1], "Best Path: {}".format(best_sub)
            elif len(subline_line) == 7:
                break

我的輸出(不確定是否正確,提供所以你可以判斷)

Found Starting Network 10.0.1.0/24 10.8.111.45 Best Path: False
Sub: 10.8.11.45 Best Path: True
Sub: 10.8.11.45 Best Path: False
Sub: 10.8.11.45 Best Path: False
Found Starting Network 10.0.3.0/29 10.8.111.45 Best Path: False
Sub: 10.8.11.45 Best Path: True
Sub: 10.8.11.45 Best Path: False
Sub: 10.8.11.45 Best Path: False
Found Starting Network 10.8.0.0/16 10.9.0.1 Best Path: False
Sub: 10.8.0.1 Best Path: False
Sub: 0.0.0.0 Best Path: True
Found Starting Network 10.9.0.0/16 10.9.0.1 Best Path: False
Sub: 10.8.0.1 Best Path: False
Sub: 0.0.0.0 Best Path: True
Found Starting Network 10.10.2.0/24 10.8.10.2 Best Path: True
Sub: 10.8.10.2 Best Path: False
Sub: 10.8.10.2 Best Path: False
Found Starting Network 10.10.5.0/24 10.8.142.15 Best Path: False
Sub: 10.8.42.15 Best Path: True
Sub: 10.8.42.15 Best Path: False
Sub: 10.8.42.15 Best Path: False
Found Starting Network 10.10.7.0/24 10.8.40.84 Best Path: True
Sub: 10.8.40.84 Best Path: False
Sub: 10.8.40.84 Best Path: False
Found Starting Network 10.10.8.0/24 10.8.10.8 Best Path: True
Sub: 10.8.110.8 Best Path: False
Sub: 10.8.10.8 Best Path: False
Sub: 10.8.10.8 Best Path: False
Found Starting Network 10.10.11.0/24 10.8.42.8 Best Path: True
Sub: 10.8.42.8 Best Path: False
Sub: 10.8.42.8 Best Path: False
Sub: 10.9.42.8 Best Path: False
Found Starting Network 10.10.12.0/24 10.8.10.12 Best Path: False
Sub: 10.8.10.12 Best Path: False
Sub: 10.8.10.12 Best Path: True

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM