我在使用 Python 程序 Cupcake ToFU 时遇到了这个问题,完整源代码可在https://github.com/Magdoll/cDNA_Cupcake/blob/master/cupcake/io/BioReaders.py 获得。该程序不断引发 KeyError 并崩溃。当我查看代码时,我看到脚本使用了“除了 KeyError”来避免程序崩溃,但是当我尝试运行它时,程序仍然因 KeyError 而崩溃。有没有人可以帮助我?这是错误信息 File "/Users/wuyibo/anaconda2/lib/python2.7/site-packages/cupcake-6.8-py2.7-macosx-10.6-x86_64.egg/cupcake/io/BioReaders.py", line 516, in process self.qLen = query_len_dict[self.qID]KeyError: 'c2468/f1p1/3565'这是从第 507 行到第 518 行的代码:if query_len_dict is not None: # over write qLen and qCoverage, should be done LAST try: self.qLen = query_len_dict[self.qID] except KeyError: # HACK for blasr's extended qID k = self.qID.rfind('/') if k >= 0: try: self.qLen = query_len_dict[self.qID[:self.qID.rfind('/')]] except KeyError: self.qLen = query_len_dict[self.qID] else: raise Exception, "Unable to find qID {0} in the input fasta/fastq!".format(self.qID)使用“除了 KeyError”,程序不应该崩溃,但是当我运行它时,它仍然因 KeyError 而崩溃。
2 回答

哆啦的时光机
TA贡献1779条经验 获得超6个赞
当你KeyError与qID == 'c2468/f1p1/3565'它就会进入不同的块。然后在 try 块中(第 514 行),它会变成新的KeyError,然后在 except 块中,它尝试执行最初将它带到这里的相同(因为 self.qID 永远不会改变)(即第 516 行与第 509 行相同) .
>>> 'c2468/f1p1/3565'[:'c2468/f1p1/3565'.rfind('/')]
'c2468/f1p1'
可以query_len_dict有钥匙'c2468/f1p1'吗?

HUX布斯
TA贡献1876条经验 获得超6个赞
我尝试了几种不同的方法,这是最简单的方法:
if self.qID in query_len_dict:
print(query_len_dict[self.qID])
else:
query_len_dict[self.qID]=0
#query_len_dict.get(self.qID, 0)#this one works too instead of the last 4 lines
这也适用于字符串,而不仅仅是数字(int,float)参数
添加回答
举报
0/150
提交
取消