无法从XML文件中读取兄弟姐妹的兄弟姐妹

问题描述 投票:-2回答:1

我想从xml文件中读取PMID和作者forename,示例如下所示

我得到了PMID和forename,但循环是PMID的次数,我想要1个PMID和各自的名字

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE PubmedArticleSet SYSTEM "http://dtd.nlm.nih.gov/ncbi/pubmed/out/pubmed_190101.dtd">
<PubmedArticleSet>
<PubmedArticle>
    <MedlineCitation Status="MEDLINE" Owner="NLM">
        <PMID Version="1">2844048</PMID>
        <AuthorList CompleteYN="Y">
            <Author ValidYN="Y">
                <LastName>Guarner</LastName>
                <ForeName>J</ForeName>
                <Initials>J</Initials>
            </Author>
            <Author ValidYN="Y">
                <LastName>Cohen</LastName>
                <ForeName>C</ForeName>
                <Initials>C</Initials>
            </Author>
            <Author ValidYN="Y">
                <LastName>Mushi</LastName>
                <ForeName>E</ForeName>
                <Initials>F</Initials>
            </Author>
        </AuthorList>
    </MedlineCitation>
</PubmedArticle>
<PubmedArticle>
    <MedlineCitation Status="MEDLINE" Owner="NLM">
        <PMID Version="1">123456</PMID>
        <AuthorList CompleteYN="Y">
            <Author ValidYN="Y">
                <LastName>Smith</LastName>
                <ForeName>C</ForeName>
                <Initials>C</Initials>
            </Author>
            <Author ValidYN="Y">
                <LastName>Jones</LastName>
                <ForeName>E</ForeName>
                <Initials>F</Initials>
            </Author>
        </AuthorList>
    </MedlineCitation>
</PubmedArticle>
</PubmedArticleSet>

代码,我试过了

FN=[]
for pmid in root.iter('PMID'):
    print(pmid.text)
    for id in root.findall("./PubmedArticle/MedlineCitation/Article/AuthorList"):
        for f in id.findall("./Author/ForeName"):
            fn=f.text

            x= '{},{}'.format(i, fn)
            #print(x)
            FN.append(x)

预期产出

PMID               AUTHORS
2844048            'Guarner J J', 'Cohen C C'
python xml python-3.x python-2.7 xml-parsing
1个回答
0
投票

我不知道您是否希望输出采用特定格式。但是,您可以尝试以下代码。输出是一个Dictionary,其中Keys是PMID,Values是作者列表。

import xml.etree.ElementTree as ET
import pandas as pd
tree = ET.parse('E:\Python\DataFiles\PMID.xml') # change according to your location
authors_pmid = []
all_authors_pmid = []
root = tree.getroot()
for amedlinecitation in root.iter('MedlineCitation'): #PMID and Author are childs of MedlineCitation
    pmid = amedlinecitation.find('PMID').text
    for anauthor in amedlinecitation.iter('Author'): # for each amedlinecitation, find all its Authors
        author_name = anauthor.find('LastName').text # for each Author, find the LastName tag and extract its value
        authors_pmid = [pmid,author_name]
        all_authors_pmid.append(authors_pmid)
df = pd.DataFrame(all_authors_pmid,columns=['PMID','Author'])
print(df)

输出:

{'2844048': ['Guarner', 'Cohen', 'Mushi'], '123456': ['Smith', 'Jones']}

以下代码将使用Python Dataframe以表格形式提供输出。

import xml.etree.ElementTree as ET
import pandas as pd
tree = ET.parse('E:\Python\DataFiles\PMID.xml') # change according to your location
authors_pmid = []
all_authors_pmid = []
root = tree.getroot()
for amedlinecitation in root.iter('MedlineCitation'): #PMID and Author are childs of MedlineCitation
    pmid = amedlinecitation.find('PMID').text
    for anauthor in amedlinecitation.iter('Author'): # for each amedlinecitation, find all its Authors
        author_name = anauthor.find('LastName').text # for each Author, find the LastName tag and extract its value
        authors_pmid = [pmid,author_name]
        all_authors_pmid.append(authors_pmid)
df = pd.DataFrame(all_authors_pmid,columns=['PMID','Author'])
print(df)

输出:

      PMID   Author
0  2844048  Guarner
1  2844048    Cohen
2  2844048    Mushi
3   123456    Smith
4   123456    Jones

上面的代码与第一个代码有何不同:

  1. 对于每对PMID和作者名称,它将创建一个列表。此列表名为authors_pmid。例如,['2844048','Guarner'],['2844048','Cohen'],['2844048','Mushi'],['123456','Smith'],['123456','Jones在内部for循环的每次迭代期间,']将是列表变量authors_pmid中的值。
  2. 然后,上述每个列表都将附加到all_authors_pmid定义的最终列表中
  3. 此最终列表将是对Dataframe构造函数的调用输入,以创建具有列名称的Dataframe:PMID和Author
© www.soinside.com 2019 - 2024. All rights reserved.