Thursday, April 15, 2010

Make bib file from Pubmed-ID

Today, I wrote a code of making bib-file from pubmed in Python. HML.


def pm2bib(pm):
    author = "; ".join(pm['AU'])
    year = pm['DP'][0:4]
    bib = """@article{PMID:%s,
author = {%s},
title = {%s},
journal = {%s},
year = {%s},
volume = {%s},
number = {%s},
pages = {%s},
}
""" % (pm['PMID'],author,pm['TI'],pm['TA'],year,pm['VI'],pm.get('IP',""),pm['PG'])
    return bib

if __name__ == "__main__":

    from Bio import Entrez, Medline

    Entrez.email = "xxx@gmail.com"
    record = Entrez.read(Entrez.esearch(db="pubmed",term="biopython"))
    idlist = record["IdList"]
    records = Medline.parse(Entrez.efetch(db="pubmed",id=idlist,rettype="medline",retmode="text"))

    for record in records:
        print pm2bib(record)

No comments: