Last active
December 23, 2015 00:03
-
-
Save hashemi/7b58899f75432d8b9ef7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Takes a PubMed ID (PMID) and your email and returns | |
# a citation more or less in Vancouver style. | |
# | |
# Depends on biopython: | |
# $ pip install biopython | |
# | |
def citation(PMID, email): | |
from Bio import Entrez | |
Entrez.email = email | |
handle = Entrez.efetch(db="pubmed", id=PMID, retmode="xml") | |
data = Entrez.read(handle) | |
article_data = data[0]["MedlineCitation"]["Article"] | |
journal_data = article_data["Journal"] | |
journal_issue_data = journal_data["JournalIssue"] | |
authors = [] | |
for author in article_data["AuthorList"]: | |
author_name = '' | |
try: | |
author_name = author["LastName"] | |
except KeyError: | |
continue | |
try: | |
author_name += ' ' + author["Initials"] | |
except KeyError: | |
pass | |
authors.append(author_name) | |
cite = { | |
"journal": str(journal_data["ISOAbbreviation"]), | |
"volume": str(journal_issue_data["Issue"]), | |
"issue": str(journal_issue_data["Volume"]), | |
"month": str(journal_issue_data["PubDate"]["Month"]), | |
"year": str(journal_issue_data["PubDate"]["Year"]), | |
# remove trailing period after a title if it already exists | |
"title": str(article_data["ArticleTitle"]).rstrip("."), | |
"authors": ", ".join(authors), | |
"pagination": str(article_data["Pagination"]["MedlinePgn"]), | |
} | |
return "{authors}. {title}. {journal}. {year} {month};{volume}({issue}):{pagination}.".format(**cite) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment