1

我不明白为什么,当我运行我的代码时,if 语句下的 for each 循环没有运行。即使找到的数量大于0!

def findpattern(commit_msg):
    pattern = re.compile("\w\w*-\d\d*")
    group = pattern.finditer(commit_msg)
    found = getIterLength(group)
    print found
    if found > 0:
        issues = 0
        for match in group:
                print " print matched issues:"
                auth = soap.login(jirauser,passwd)

                print match.group(0)
                getIssue(auth,match.group(0))
                issues = issues + 1
    else: 
        sys.exit("No issue patterns found.")

print "Retrieved issues: " + str(issues)  

任何帮助将不胜感激,我已经为此努力了一个小时。

4

2 回答 2

8

您的getIterLength()函数通过耗尽返回的迭代器来查找长度finditer()。然后,您将需要 for 循环的新迭代器实例。相反,我会像这样重组你的代码:

def findpattern(commit_msg):
    pattern = re.compile("\w\w*-\d\d*")
    group = pattern.finditer(commit_msg)

    found = 0
    issues = 0
    for match in group:
        print " print matched issues:"
        auth = soap.login(jirauser,passwd)

        print match.group(0)
        getIssue(auth,match.group(0))
        issues = issues + 1
        found += 1
    if found == 0:
        sys.exit("No issue patterns found.")


    print "Retrieved issues: " + str(issues)  

或者,您可以使用该findall()方法而不是finditer()给您一个列表(这是一个可迭代的,而不是迭代器),您可以在该列表上运行len(group)以获取大小,然后使用它在您的 for 循环中进行迭代。

于 2010-07-27T20:48:40.340 回答
1

检查你的代码格式,因为你有一个双标签而不是单标签,记住 python 对缩进非常挑剔

于 2010-07-27T20:34:41.537 回答