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 assigning です。

4

2 に答える 2

2

発生しているエラーは、startおよびend変数が原因です。値が設定されていないイベントに存在するように、最初に初期化してみてください。

さらに、2 つの異なる変数にアンパックされる文字列を作成して返そうとしています。

次のことを試してください。

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 に答える