Question: Need Help Writing Biopython to CSV
gravatar for pmramos95
13 months ago by
pmramos950 wrote:

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 = ""

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 =

pmid_list = record["IdList"]


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']

        date = record['DP']

        place = record['PL']
    # Close the efetch handle    

    # print final journal name and coun
    for title in journal_dict:
    for date in datep_dict:
    for place in place_dict:


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!

ADD COMMENTlink modified 13 months ago by Vimalkumar Velayudhan190 • written 13 months ago by pmramos950

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 your records loop ? An alternative if you want to do stats on your records, append record 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 :

Dataframe to csv :

ADD REPLYlink modified 13 months ago • written 13 months ago by Bastien HervĂ©4.3k
gravatar for Vimalkumar Velayudhan
13 months ago by
Vimalkumar Velayudhan190 wrote:

You can use DictWriter class in the csv module to write a CSV file.

ADD COMMENTlink written 13 months ago by Vimalkumar Velayudhan190
Please log in to add an answer.


Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1701 users visited in the last hour