在python中处理异常并继续执行for循环

在python中处理异常并继续执行for循环,python,bioinformatics,continue,custom-error-handling,bioservices,Python,Bioinformatics,Continue,Custom Error Handling,Bioservices,我有以下代码: import Bio from bioservices import KEGGParser, UniProt, QuickGO #start a new method of my custom 'Retrieve_Data' class def locate_common_GO(self,list_of_genes,GO_term): #initialize variables and classes q = QuickGO() a = Ret

我有以下代码:

import Bio
from bioservices import KEGGParser, UniProt, QuickGO

#start a new method of my custom 'Retrieve_Data' class
def locate_common_GO(self,list_of_genes,GO_term):
    
    #initialize variables and classes
    q = QuickGO()
    a = Retrieve_Data()
    b=[]
使用(自定义方法)
hugo2uniprot

for i in range(0,len(list_of_genes)):
    b.append(a.hugo2uniprot(list_of_genes[i],'hsa'))
    print 'Gene: {} \t UniProtID: {}'.format(list_of_genes[i],b[i])
    
搜索GO术语(使用
bioservices
QuickGO
)并存储为字典

GO_dict = {}
q = QuickGO()
我拥有的一些大的基因名称列表没有返回任何命中率。 这会导致我要处理的
AttributeError
,然后转到列表中的下一项。当前代码返回错误:

AttributeError:“int”对象没有属性“split”

错误源于
bioservices
模块(因此当我调用
q.Annotation
时)


是否有人建议让异常处理正常工作,以便它完成对指定列表的迭代,而不是停止脚本?

好的,我刚刚意识到问题是错误来自不同的代码位。。。所以我基本上什么都不想处理。我现在已经修好了,感谢所有的回应者

属性错误的详细信息是什么?您所说的“不工作”是什么意思?此外,在Python中,使用
range()
而不是直接在列表上循环是一种代码味道。也许您希望这样您就有了一个索引计数器?它是否引发了除
AttributeError
之外的异常?是什么导致脚本停止执行?更多信息会很有用。@Tom-你是想告诉我enumerate()将做与range(n,m)相同的工作吗?@slurms-我编辑了这个问题,试图提供更多细节,也许它在第一个循环中失败了(使用(自定义方法)hugo2uniprot获取uniprot ID)。它能穿透所有的基因吗?它不应该在第二天失败,因为循环正在显式地捕获
AttributeError
。。。
for i in range(len(b)):
    try:
        GO_dict[list_of_genes[i]] = q.Annotation(protein=b[i], frmt="tsv", _with=True,tax=9606, source="UniProt", col="goName")
    except AttributeError:
        continue

# The rest of this code is irrelevant to my problem but I've included it for completeness: 
# Essentially it is designed to locate specific keywords in the results of the above code and return the gene name that they came from. 
keys = GO_dict.keys()
matches = []
for gene in range(0,len(keys)):
    if GO_term in GO_dict[keys[gene]].splitlines():
        matches.append(keys[gene])
return matches