Biopython for Loop IndexError

时间:2016-03-09 21:21:35

标签: loops for-loop biopython

当我输入此代码时,我得到“IndexError:list超出范围”。此外,retmax设置为614,因为这是我发出请求时的结果总数。有没有办法使用根据搜索结果而变化的变量使retmode等于结果数?

#!/usr/bin/env python

from Bio import Entrez
Entrez.email = "something@gmail.com"
handle1 = Entrez.esearch(db = "nucleotide", term = "dengue full genome", retmax = 614) 
record = Entrez.read(handle1)
IdNums = [int(i) for i in record['IdList']]

while i >= 0 and i <= len(IdNums):
handle2 = Entrez.esearch(db = "nucleotide", id = IdNums[i], type = "gb", retmode = "text")
record = Entrez.read(handle2)
print(record)
i += 1

1 个答案:

答案 0 :(得分:0)

您可以使用for循环...

,而不是使用while循环
from Bio import Entrez
Entrez.email = 'youremailaddress'
handle1 = Entrez.esearch(db = 'nucleotide', term = 'dengue full genome', retmax = 614)
record = Entrez.read(handle1)
IdNums = [int(i) for i in record['IdList']]

for i in IdNums:
   print(i)
   handle2 = Entrez.esearch(db = 'nucleotide', term = 'dengue full genome', id = i, rettype = 'gb', retmode = 'text')
   record = Entrez.read(handle2)
   print(record)

我在电脑上运行它似乎工作。 for循环解决了越界问题,并将句子添加到handle2解决了调用错误。

相关问题