Python 如何在不进入 For 循环的情况下检查下一个 iter 项

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

当通过 next(iter) 进行迭代时,我想基本上将网络组合在一起,我通过计算每一行中出现的项目数来做到这一点,如果它有 7 个项目,它就是一个条目的开始。

然后我在输入行下方查找行,直到找到包含 7 个项目的另一行。然后该过程重新开始。

唯一的问题是当我通过 line = next(bgp_table_iter) 访问 "next line" 并打破循环时,如果 "next line" 有 7 个项目意味着开始一个新条目,当它重新开始时在 for line in bgp_table_iter: 它再次迭代到下一行,跳过原来的 "next line" 应该有 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(): 数据 = test_output_very_short.split('RPKI validation codes: V valid, I invalid, N Not found\n\n Network Next Hop Metric LocPrf Weight Path\n') bgp_table = 数据[1] bgp_table = re.sub(" 网络下一跃点度量 LocPrf 权重 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 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 show here 打印出正在检查的行的索引。

不要强迫它成为 iter,而是继续将其用作 list,然后使用 enumerate 来了解您在 list 中的位置。

编辑:更改第一个枚举以使用 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