python – 如何使函数返回每个可能的值

所以我正在使用这种方法,它在查找数字方面工作正常,但它只返回最后一个值.是否有一种方法可以使它在每次运行后返回所有值.
这是我的代码:

def searchPFAM():

    fileAddress = '/Volumes/interpro/data/Q14591.txt'
    start = None
    end = None
    with open(fileAddress,'rb') as f:
        root = etree.parse(f)
        for lcn in root.xpath("/protein/match[@dbname='PFAM']/lcn"):#find dbname =PFAM
            start = int(lcn.get("start"))#if it is PFAM then look for start value
            end = int(lcn.get("end"))#if it is PFAM then also look for end value
            print start, end
        return start, end

解决方法:

你是说与此类似的东西吗?

def do_something(fname):
    with open(fname,'rb') as f:
        root = etree.parse(f)
        for lcn in root.xpath("/protein/match[@dbname='PFAM']/lcn"):#find dbname =PFAM
            # Make slightly more robust
            try:
                start = int(lcn.get("start"))#if it is PFAM then look for start value
                end = int(lcn.get("end"))#if it is PFAM then also look for end value
                yield start, end
            except (TypeError , ValueError) as e:
                pass # start/end aren't usable as numbers decide what to do here...

for start, end in do_something():
    do_something_else(start, end)
上一篇:python-文档内模式声明和lxml


下一篇:lxml webdriver 抓取街拍