0

我想知道,有没有一种方法可以在我的主要方法上存储开始值和结束值。我尝试这样做,但它给了我错误:

def searchM():

    fileAddress = '/database/pro/data/'+ID+'.txt'
    with open(fileAddress,'rb') as f:
        root = etree.parse(f)
        for lcn in root.xpath("/protein/match[@dbname='M']/lcn")
            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
    return "%s, %s" % (start, end,)

values = searchM()

(start, end,) = values

错误消息是 UnboundLocalError: local variable 'start' referenced before assignment

4

2 回答 2

2

您遇到的错误是由startandend变量引起的。尝试先初始化它们,以便它们即使在未设置值的情况下也存在。

此外,您正在尝试创建并返回一个字符串,然后将其解压缩为两个不同的变量。

尝试以下操作:

def searchM():
    fileAddress = '/database/pro/data/%s.txt' % ID
    start = None
    end = None
    with open(fileAddress,'rb') as f:
        root = etree.parse(f)
        for lcn in root.xpath("/protein/match[@dbname='M']/lcn"):
            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
    return start, end

(start, end) = searchM()  
于 2012-07-10T22:57:10.090 回答
1

如果未找到start,您需要为 提供值:end

for lcn in root.xpath("/protein/match[@dbname='M']/lcn"):
    start = int(lcn.get("start"))
    end = int(lcn.get("end"))
    break
else: # not found
    start = end = None
于 2012-07-10T23:18:24.350 回答