【问题标题】:Python: For loop only loops over the first part of a txt filePython:For循环仅循环txt文件的第一部分
【发布时间】:2014-04-29 07:40:37
【问题描述】:

最近我不得不为我的实习编写一个脚本来检查是否在一堆路由器/交换机配置中出现子网。

我制作了一个生成输出的脚本。现在我需要第二个脚本(我无法让它工作),它读取输出,如果出现子网将其写入aanwezig.txt,如果不写入nietAanwezig.txt

许多其他答案帮助我制作了这个脚本并且它可以工作,但它只对前 48 个 IP 执行,并且有超过 2000 个...

checkOutput.py的代码:

def main():

    file = open('../iprangesclean.txt', 'rb')
    aanwezig = open('../aanwezig.txt', 'w')
    nietAanwezig = open('../nietAanwezig.txt', 'w')
    output = open('output.txt', 'rb')

    for line in file:
        originalLine = line
        line.rstrip()
        line = line.replace(' ', '')
        line = line.replace('\n', '')
        line = line.replace('\r', '')
        one,two,three,four = line.split('.')

        # 3Byte IP:
        ipaddr = str(one) + "." + str(two) + "." + str(three)

        counter = 1
        found = 0   
        for lijn in output:

            if re.search("\b{0}\b".format(ipaddr),lijn) and found == 0:
                found = 1                               
            else:
                found = 2

            print counter
            counter= counter + 1

        if found == 1:
            aanwezig.write(originalLine)
            print "Written to aanwezig"
        elif found == 2:
            nietAanwezig.write(originalLine)
            print "Written to nietAanwezig"
        found = 0

    file.close()
    aanwezig.close()
    nietAanwezig.close()


main()

iprangesclean.txt 的格式如下:

10.35.6.0/24
10.132.42.0/24
10.143.26.0/24
10.143.30.0/24
10.143.31.0/24
10.143.32.0/24
10.35.7.0/24
10.143.35.0/24
10.143.44.0/24
10.143.96.0/24
10.142.224.0/24
10.142.185.0/24
10.142.32.0/24
10.142.208.0/24
10.142.70.0/24
and so on...

output.txt的一部分(我不能给你所有的东西,因为它有用户信息):

*name of device*.txt:logging 10.138.200.100
*name of device*.txt:access-list 37 permit 10.138.200.96 0.0.0.31
*name of device*.txt:access-list 38 permit 10.138.200.100
*name of device*.txt:snmp-server host 10.138.200.100 *someword*
*name of device*.txt:logging 10.138.200.100

【问题讨论】:

  • 那不是 Python 程序。编辑问题并更正缩进。
  • 我们不知道output.txt
  • 附带说明,您应该首先考虑清理代码并删除多余的行。也许像:gist.github.com/anonymous/11394195
  • 能否提供output.txt的第46到50行?

标签: python file loops


【解决方案1】:

试试这个改变:

for lijn in output:
    found = 0 # put this here
    if re.search("\b{0}\b".format(ipaddr),lijn) and found == 0:
        found = 1
    else:
        found = 2

    print counter
    counter= counter + 1

    """Indent one level so it us in the for statement"""
    if found == 1:
        aanwezig.write(originalLine)
        print "Written to aanwezig"
    elif found == 2:
        nietAanwezig.write(originalLine)
        print "Written to nietAanwezig"

如果我正确理解了这个问题,这应该会引导您走向正确的方向。 if statement 当前未在 for statement 中执行。如果这确实解决了您的问题,那么您也不需要 found 变量。你可以有类似的东西:

for counter, lijn in enumerate(output, 1):
    if re.search("\b{0}\b".format(ipaddr),lijn):
        aanwezig.write(originalLine)
        print "Written to aanwezig"
    else:
        nietAanwezig.write(originalLine)
        print "Written to nietAanwezig"

    print counter

如果我误解了这个问题,请告诉我。

注意上面的代码我没有测试过,作为起点试试吧。

【讨论】:

  • 我在 for 循环中实现了 if found == 1 .... 它似乎可以正常工作,但仍然存在同样的问题:他只执行 2337 的前 48 行...我已经仔细检查过,iprangesclean.txt 中的第 48 行周围没有空格或任何内容
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-28
  • 2021-09-26
相关资源
最近更新 更多