Skip to content

Instantly share code, notes, and snippets.

@informationsea
Created September 15, 2022 13:45
Show Gist options
  • Save informationsea/ea6f32c76b4fde6bf2785c6b345edf98 to your computer and use it in GitHub Desktop.
Save informationsea/ea6f32c76b4fde6bf2785c6b345edf98 to your computer and use it in GitHub Desktop.
Format Pubmed for Citaion
#!/usr/bin/env python3
import argparse
import xml.etree.ElementTree as ET
def _main():
parser = argparse.ArgumentParser(description='')
parser.add_argument('pubmed_xml')
options = parser.parse_args()
tree = ET.parse(options.pubmed_xml)
root = tree.getroot()
print(root)
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)
print(
f"{author_text} ({pub_year}) \"{title}\" {journal}. https://doi.org/{doi}")
pass
if __name__ == '__main__':
_main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment