Entering edit mode
6.6 years ago
pmramos95
•
0
Hi, I am currently working on a class project that requires me to pull data from the NCBI website using biopython and writing it to a CSV file which I then analyze in R. I got all the data I need, but I'm not completely sure how to write it into a CSV file since we never covered it in class. Here is my code so far:
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)
GAP
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 a journal name
title = record['JT']
journal_dict.append(title)
date = record['DP']
datep_dict.append(date)
place = record['PL']
place_dict.append(place)
# Close the efetch handle
handle.close()
# print final journal name and coun
for title in journal_dict:
print(title)
for date in datep_dict:
print(date)
for place in place_dict:
print(place)
GAP
import csv
What I would like my xcel sheet to look like would be
[ID, Journal Title, Publication Date, Place of Publication]
[123, Title1, Date1, Place1]
[124, Title2, Date2, Place2]
etc. Any help would be greatly appreciated!
As it is a school assignment, it's not in the policy to give you point by point the answer. Moreover, instead of dictionnary appending why not write your
record
in csv in yourrecords
loop ? An alternative if you want to do stats on yourrecords
, appendrecord
in a dataframe then outside of the for loop do your stats on the dataframe, then, write dataframe in csv.Some examples :
To write in loop : http://zetcode.com/python/csv/
Dataframe to csv : https://stackoverflow.com/questions/16923281/pandas-writing-dataframe-to-csv-file