我目前正在开展一个类项目,要求我使用biopython从NCBI网站提取数据并将其写入CSV文件,然后我在R中进行分析。我得到了我需要的所有数据,但我不是完全确定如何将其写入CSV文件,因为我们从未在课堂上介绍它。到目前为止,这是我的代码:
from Bio import Entrez, Medline
Entrez.email = "email.here"
handle = Entrez.esearch(db="pubmed", # database to search
term="Chan CS[Author] AND 2000:2017[Date - Publication]", # search term
retmax=200 # Maximum number of results to return
)
record = Entrez.read(handle)
handle.close()
pmid_list = record["IdList"]
print(pmid_list)
其次是
from Bio import Medline
handle = Entrez.efetch(db="pubmed", id=pmid_list, rettype="medline", retmode="text")
records = Medline.parse(handle)
journal_dict = []
datep_dict = []
place_dict = []
for record in records:
# retrieve journal titles
title = record['JT']
journal_dict.append(title)
#retrieve date published
date = record['DP']
datep_dict.append(date)
#retrieve place published
place = record['PL']
place_dict.append(place)
# Close the efetch handle
handle.close()
for title in journal_dict:
print(title)
for date in datep_dict:
print(date)
for place in place_dict:
print(place)
最后,我被困在
部分import csv
我正在尝试让csv文件看起来像下面的
[ID, Journal Title, Publication Date, Place of Publication]
[123, Title1, Date1, Place1]
[124, Title2, Date2, Place2]
非常感谢任何帮助!
答案 0 :(得分:0)
在您的第二个代码块中,您的变量名称是关于dict
离子的,但它们实际上是list
s:
journal_dict = []
datep_dict = []
place_dict = []
所以,让我们解决这个问题(这在以后写入CSV时也会有用):
record_list = []
for record in records:
record_dict = {'ID': record['ID'],
'Journal Title': record['JT'],
'Publication Date': record['DP']
'Place of Publication': record['PL']}
record_list.append(record_dict)
现在让我们将这个词典列表写入CSV文件
import csv
with open('medline.csv', 'w', newline='') as csvfile:
fieldnames = ['ID', 'Journal Title', 'Publication Date', 'Place of Publication']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for record_dict in record_list:
writer.writerow(record_dict)