解析 pumbed的xml

meta分析中需要调研大量文献,  pubmed数据库是主要来源, 但是pubmed 导出的文本文件,可读性非常差,并且考虑到需要对文献进行筛选和标记,xlsx是个很好的选择. 下面代码是把 xml 转化为 xlsx

 

#!/usr/bin/env python3
'''
解析 pubmed 导出的xml文件,并且转换成xlsx格式
'''
import sys
import xml.etree.ElementTree as ET
import pandas as pd
import numpy as np


def main(file, out):
    tree = ET.parse(file)
    root = tree.getroot()
    PMID = None
    ArticleTitle = None
    Journal_name = None
    Abstract = None
    First_author = None
    First_author_Affiliation = None
    DOI = None
    Year = None
    df = pd.DataFrame(columns=["PMID", "DOI", "Journal", "Year",
                               "First_author", "First_author_affiliation", "Title", "Abstract"])
    for pubmedArticle in root:
        try:
            PMID = pubmedArticle.find("MedlineCitation/PMID").text
            ArticleTitle = pubmedArticle.find(
                "MedlineCitation/Article/ArticleTitle").text
            Journal_name = pubmedArticle.find(
                "MedlineCitation/Article/Journal/Title").text
            Abstract = pubmedArticle.find(
                "MedlineCitation/Article/Abstract/AbstractText").text
            First_author = pubmedArticle.find(
                "MedlineCitation/Article/AuthorList/Author")[1].text
            First_author_Affiliation = pubmedArticle.find(
                "MedlineCitation/Article/AuthorList/Author[1]/AffiliationInfo/Affiliation").text
            DOI = pubmedArticle.find(
                "MedlineCitation/Article/ELocationID").text
            Year = pubmedArticle.find("MedlineCitation/DateRevised/Year").text
        except:
            pass
        line = {"PMID": PMID, "DOI": DOI, "Journal": Journal_name, "Year": Year, "First_author": First_author,
                "First_author_affiliation": First_author_Affiliation, "Title": ArticleTitle, "Abstract": Abstract}

        #line = [[str(x) for x in [PMID,ArticleTitle,Journal_name,Abstract,First_author,First_author_Affiliation,DOI,Year]]]
        df = df.append(line, ignore_index=True)
    df.to_excel(out, index=False)


if __name__ == "__main__":
    if len(sys.argv[1:]) < 2:
        print("py pubmed_result.xml out.xlsx")
        sys.exit(1)
    main(sys.argv[1], sys.argv[2])

 

你可能感兴趣的:(NGS,python应用)