使用 xml.etree 解析 Python xml [英] Python xml parsing with xml.etree

查看:85
本文介绍了使用 xml.etree 解析 Python xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 的名称

I want to get names for each PubmedArticle

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

but this code gets the whole name at once

<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 划分名称

How can I get names divied by PMID like this

[结果]

PMID 1:马卡尔、麦克马丁

PMID 1: Makar, McMartin

PMID 2:苍白、泰弗利

PMID 2: Palese, Tephly

推荐答案

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

pubmed and name are two independent lists. You have to query the authors for each article seperatelly:

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')

这篇关于使用 xml.etree 解析 Python xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆