【发布时间】: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