将 Biopython 输出写入 csv
Writing Biopython output into csv
我目前正在做一个 class 项目,该项目要求我使用 biopython 从 NCBI 网站提取数据并将其写入 CSV 文件,然后我在 R 中对其进行分析。我得到了我需要的所有数据, 但我不完全确定如何将其写入 CSV 文件,因为我们从未在 class 中介绍过它。到目前为止,这是我的代码:
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]
如有任何帮助,我们将不胜感激!
在你的第二个代码块中,你的变量名谈论 dict
ionaries,但你的它们实际上是 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)
我目前正在做一个 class 项目,该项目要求我使用 biopython 从 NCBI 网站提取数据并将其写入 CSV 文件,然后我在 R 中对其进行分析。我得到了我需要的所有数据, 但我不完全确定如何将其写入 CSV 文件,因为我们从未在 class 中介绍过它。到目前为止,这是我的代码:
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]
如有任何帮助,我们将不胜感激!
在你的第二个代码块中,你的变量名谈论 dict
ionaries,但你的它们实际上是 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)