Question: Need Help Writing Biopython to CSV
0
gravatar for pmramos95
10 weeks ago by
pmramos950
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

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!

ADD COMMENTlink modified 10 weeks ago by Vimalkumar Velayudhan180 • written 10 weeks ago by pmramos950
3

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 REPLYlink modified 10 weeks ago • written 10 weeks ago by Bastien HervĂ©1.5k
1
gravatar for Vimalkumar Velayudhan
10 weeks ago by
Ireland
Vimalkumar Velayudhan180 wrote:

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 COMMENTlink written 10 weeks ago by Vimalkumar Velayudhan180
Please log in to add an answer.

Help
Access

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