Need Help Writing Biopython to CSV
1
0
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!

biopython csv entrez schoolassignment • 2.4k views
ADD COMMENT
3
Entering edit mode

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 : http://zetcode.com/python/csv/

Dataframe to csv : https://stackoverflow.com/questions/16923281/pandas-writing-dataframe-to-csv-file

ADD REPLY
1
Entering edit mode
6.6 years ago
vimalkvn ▴ 320

You can use DictWriter class in the csv module to write a CSV file. https://docs.python.org/3/library/csv.html#csv.DictWriter

ADD COMMENT

Login before adding your answer.

Traffic: 2497 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6