生物粒子计数E

2024-05-15 22:31:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在进行一个项目,我需要从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
...

Tags: pathinfromimportabstractidtitlemy
1条回答
网友
1楼 · 发布于 2024-05-15 22:31:00

你的错误在

handle = Entrez.efetch(db="pubmed",rettype="medline",retmode="text", id=row.id)

从文档中

id

UID list. Either a single UID or a comma-delimited list of UIDs

From the examples I seeid是一个字符串,而不是数据帧中的numpy.int64。你应该把row.id转换成一个字符串

相关问题 更多 >