我目前正在开展一个项目,我需要从PubMed下载几千个引文。我目前正在使用BioPython并编写了这段代码:
from Bio import Entrez
from Bio import Medline
from pandas import *
from sys import argv
import os
Entrez.email = "my_email"
df = read_csv("my_file_path")
i=0
for index, row in df.iterrows():
print (row.id)
handle = Entrez.efetch(db="pubmed",rettype="medline",retmode="text", id=row.id)
records = Medline.parse(handle)
for record in records:
try:
abstract = str(record["AB"])
except:
abstract = "none"
try:
title = str(record["TI"])
except:
title = "none"
try:
mesh = str(record["MH"])
except:
mesh = "none"
path = 'my_file_path'
filename= str(row.id) + '.txt'
filename = os.path.join(path, filename)
file = open(filename, "w")
output = "title: "+str(title) + "\n\n" + "abstract: "+str(abstract) + "\n\n" + "mesh: "+str(mesh) + "\n\n"
file.write(output)
file.close()
print (i)
i=i+1
但是,运行此代码时收到以下错误:
Traceback (most recent call last):
File "my_file_path", line 13, in <module>
handle = Entrez.efetch(db="pubmed",rettype="medline",retmode="text", id=row.id)
File "/.../anaconda/lib/python3.5/site-packages/biopython-1.68-py3.5-macosx-10.6-x86_64.egg/Bio/Entrez/__init__.py", line 176, in efetch
if ids.count(",") >= 200:
AttributeError: 'numpy.int64' object has no attribute 'count'
以下是CSV文件的前几列:
id
10029645
10073846
10078088
10080457
10088066
...
答案 0 :(得分:1)
您的错误发生在
handle = Entrez.efetch(db="pubmed",rettype="medline",retmode="text", id=row.id)
来自文档
<强> ID 强>
UID列表。单个UID或逗号分隔的UID列表
From the examples I see,id
是一个字符串,而不是pandas数据帧中的numpy.int64
。您应该将row.id
转换为字符串