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,如下所示
结果
PMID1: Makar,McMartin
PMID 2: Palese,Tephly
发布于 2021-05-04 18:47:39
pubmed和name是两个独立的列表。你必须分别查询每篇文章的作者:
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')https://stackoverflow.com/questions/67383045
复制相似问题