findall作为循环中的停止准则

2024-09-27 21:30:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图创建一个依赖于findall结果的循环。我的循环每次需要一个文本来搜索模式,如果它发现它正在做一些工作人员。上一次搜索什么也没找到时,它给了我一个错误:

cursor = re.findall(r'MDpub\w+[^\"]', f)[0] 
IndexError: list index out of range

我的python代码如下

cursor = re.findall(r'MDpub\w+[^\"]', f)[0]     
while cursor:
    next_url = 'url'
    subId = subId+1
    wget = "wget command"
    os.system(wget)
    f = open(('name of file')       
    if (re.findall(r'MDpub\w+[^\"]', f) != cursor):
        cursor = re.findall(r'MDpub\w+[^\"]', f)[0] 
        print cursor

Tags: ofreurlindex错误模式wgetout

热门问题