Skip to content

Instantly share code, notes, and snippets.

Created September 15, 2022 13:45
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Format Pubmed for Citaion
#!/usr/bin/env python3
import argparse
import xml.etree.ElementTree as ET
def _main():
parser = argparse.ArgumentParser(description='')
options = parser.parse_args()
tree = ET.parse(options.pubmed_xml)
root = tree.getroot()
for one in tree.findall('.//PubmedArticle'):
pub_year = one.find('.//PubDate/Year').text
# pub_month = one.find('.//PubDate/Month').text
# pub_day = one.find('.//PubDate/Day').text
title = one.find('.//ArticleTitle').text
journal = one.find('.//Journal/Title').text
# volume = one.find('.//Journal/JournalIssue/Volume').text
#issue = one.find('.//Journal/JournalIssue/Issue')
doi = one.find('.//ArticleId[@IdType=\'doi\']').text
author_list = []
for one_author in one.findall('.//Author'):
last_name = one_author.find('./LastName').text
initials = one_author.find('./Initials').text
author_list.append(f"{initials}, {last_name}.")
author_text = ', '.join(author_list)
f"{author_text} ({pub_year}) \"{title}\" {journal}.{doi}")
if __name__ == '__main__':
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment