【问题标题】:Python xml parsing with xml.etree使用 xml.etree 解析 Python xml
【发布时间】:2021-05-04 10:30:57
【问题描述】:
import os
from xml.etree import ElementTree
file_name = 'sex.xml'
full_file = os.path.abspath(os.path.join('data', file_name))

dom = ElementTree.parse(full_file)
pubmed = dom.findall('PubmedArticle')
name = dom.findall('PubmedArticle/AuthorList/Author')

for p in pubmed:
    pmid = p.find('PMID').text
    print('PMID: {}'.format(pmid))
    for n in name:
       LastName = n.find('LastName').text
       print('{}'.format(LastName))
    print('========\n')

我想获取每个 PubmedArticle 的名称

但这段代码会立即获得全名

<root>
  <PubmedArticle>
      <PMID>1</PMID>
      <AuthorList>
        <Author>
          <LastName>Makar</LastName>
        </Author>
        <Author>
          <LastName>McMartin</LastName>
        </Author>
       </AuthorList>
  </PubmedArticle>
  <PubmedArticle>
      <PMID>2</PMID>
      <AuthorList>
        <Author>
          <LastName>Palese</LastName>
        </Author>
        <Author>
          <LastName>Tephly</LastName>
        </Author>
       </AuthorList>
  </PubmedArticle>
</root>

我怎样才能得到这样按 PMID 划分的名称

[结果]

PMID 1:马卡尔,麦克马丁

PMID 2:帕莱塞,特弗利

【问题讨论】:

    标签: python xml parsing elementtree


    【解决方案1】:

    pubmedname 是两个独立的列表。您必须单独查询每篇文章的作者:

    articles = dom.findall('PubmedArticle')
    for article in articles:
        pmid = article.findtext('PMID')
        print(f'PMID: {pmid}')
        authors = article.findall('AuthorList/Author')
        for author in authors:
            lastname = author.findtext('LastName')
            print(lastname)
        print('========\n')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-12
      • 1970-01-01
      • 1970-01-01
      • 2021-12-26
      • 2020-11-02
      • 1970-01-01
      • 1970-01-01
      • 2021-02-06
      相关资源
      最近更新 更多