【问题标题】:My `break` doesn't work in while True loop我的`break`在while True循环中不起作用
【发布时间】:2014-05-27 13:04:06
【问题描述】:

这是我的 python 脚本的一部分:

with open("/tmp/IpList.txt", "r+") as f:
    for ip in f:
        page = 1
        ip = ip.strip("\r\n")
        print ip
        while True:
            url = 'http://www.bing.com/search?q=ip%3a' + ip + '&qs=n&pq=ip%3a' + ip + '&sc=0-0&sp=-1&sk=&first=' + str(page) + '&FORM=PERE'
            print url
            with open("/tmp/content.txt", "a") as out:
                content = ContentFunc()
                if "<cite>" in content and "</cite>" in content:
                    out.write(content)
                    with open("/tmp/result.txt", "a") as result:
                        res = CiteParser()
                        result.write(res.encode('utf-8'))
                    page += 10
                else:
                    break

IpList.txt 中的 ip 完成后,它会再次转到第一个 ip。为什么?

谢谢...

【问题讨论】:

  • break 只存在它所在的循环,而不存在“父”循环
  • 我不知道这会如何发生,除非 fContentFunc()CiteParser() 或在您没有向我们展示的其他剩余代码中发生某些事情......跨度>
  • @TimCastelijns:这个问题让我觉得他知道(“当IpList.txt用完时,它会回到第一个IP”),但问题的标题也让我感到困惑。跨度>
  • @TimPietzcker 好点,我得出的结论太快了。删除了我的答案

标签: python loops for-loop infinite-loop break


【解决方案1】:

你不需要while循环,把所有ip放在一个列表中:

 with open("a.txt", "r+") as f:
    page = 1
    ips = [ip.strip("\r\n") for ip  in f]
    for ip in ips:
        url = 'http://www.bing.com/search?q=ip%3a' + ip + '&qs=n&pq=ip%3a' + ip + '&sc=0-0&sp=-1&sk=&first=' + str(page) + '&FORM=PERE'
        print url
        with open("/tmp/content.txt", "a") as out:
            content = ContentFunc()
            if "<cite>" in content and "</cite>" in content:
                out.write(content)
                with open("/tmp/result.txt", "a") as result:
                    res = CiteParser()
                    result.write(res.encode('utf-8'))
                page += 10

【讨论】:

  • 我的 IpList.txt 有问题,所以我的 while 循环运行良好。但是你没有while True的回答让我感兴趣:),谢谢
【解决方案2】:

我同意以上说法,您可以使用:

with open("/tmp/IpList.txt", "r+") as f:
    for ip in f:
        page = 1
        ip = ip.strip("\r\n")
        print ip
        loop_going = True # New var <---------------
        while loop_going:
            url = 'http://www.bing.com/search?q=ip%3a' + ip + '&qs=n&pq=ip%3a' + ip + '&sc=0-0&sp=-1&sk=&first=' + str(page) + '&FORM=PERE'
            print url
            with open("/tmp/content.txt", "a") as out:
                content = ContentFunc()
                if "<cite>" in content and "</cite>" in content:
                    out.write(content)
                    with open("/tmp/result.txt", "a") as result:
                        res = CiteParser()
                        result.write(res.encode('utf-8'))
                    page += 10
                else:
                    loop_going = False # Set to false <---------------

【讨论】:

  • 这只是在不改变功能的情况下添加更多代码
  • 只是想表明:如果他的“break”不能在他想要的时间和地点完成工作,他可以​​使用布尔值来代替
  • 但是如果break 没有按照他的预期去做,问题不在于break '不起作用'
猜你喜欢
  • 2018-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-14
  • 1970-01-01
  • 2021-04-15
  • 2021-11-24
  • 2017-03-03
相关资源
最近更新 更多