gpt4 book ai didi

python - python 中的异常处理和继续 for 循环

转载 作者:太空宇宙 更新时间:2023-11-03 18:05:56 25 4
gpt4 key购买 nike

我有以下代码:

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获取uniprot IDS

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' object has no attribute 'split'.

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

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

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

最佳答案

好吧,我刚刚意识到问题是错误来自不同的代码...所以我基本上什么也不处理。我现在已经修复了,感谢所有回复的人。

关于python - python 中的异常处理和继续 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26912999/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com