我有一个结构如下所示的 XML 文件(为了这个问题的目的而简化)。对于每条记录,我想提取文章标题和“ArticleId”元素中包含DOI编号的属性“IdType”的值(有时这个属性可能会丢失),然后将文章标题存储在带有DOI的字典中作为关键。
<PubmedArticleSet>
<PubmedArticle>
<MedlineCitation Status="MEDLINE" Owner="NLM">
<Article PubModel="Print-Electronic">
<ArticleTitle>Malathion and dithane induce DNA damage in Vicia faba.</ArticleTitle>
</Article>
</MedlineCitation>
<PubmedData>
<ArticleIdList>
<ArticleId IdType="pubmed">28950791</ArticleId>
<ArticleId IdType="doi">10.1177/0748233717726877</ArticleId>
</ArticleIdList>
</PubmedData>
</PubmedArticle>
<PubmedArticle>
<MedlineCitation Status="MEDLINE" Owner="NLM">
<Article PubModel="Print-Electronic">
<ArticleTitle>Impact of dual inoculation with Rhizobium and PGPR on growth and antioxidant status of Vicia faba L. under copper stress.</ArticleTitle>
</Article>
</MedlineCitation>
<PubmedData>
<ArticleIdList>
<ArticleId IdType="pubmed">25747267</ArticleId>
<ArticleId IdType="pii">S1631-0691(15)00050-5</ArticleId>
<ArticleId IdType="doi">10.1016/j.crvi.2015.02.001</ArticleId>
</ArticleIdList>
</PubmedData>
</PubmedArticle>
<PubmedArticle>
<MedlineCitation Status="MEDLINE" IndexingMethod="Curated" Owner="NLM">
<Article PubModel="Print-Electronic">
<ArticleTitle>[Influence of Four Kinds of PPCPs on Micronucleus Rate of the Root-Tip Cells of Vicia-faba and Garlic].</ArticleTitle>
</Article>
</MedlineCitation>
<PubmedData>
<ArticleIdList>
<ArticleId IdType="pubmed">27548984</ArticleId>
<!-- in this record, DOI is missing -->
</ArticleIdList>
</PubmedData>
</PubmedArticle>
</PubmedArticleSet>
为了实现这一点,我使用了 xml.etree.ElementTree,如下所示:
import xml.etree.ElementTree as ET
xmldoc = ET.parse('sample.xml')
root = xmldoc.getroot()
pubs = {}
for elem in xmldoc.iter(tag='ArticleTitle'):
title = elem.text
for subelem in xmldoc.iter(tag='ArticleId'):
if subelem.get("IdType") == "doi":
doi = subelem.text
pubs[doi] = title
if len(pubs) == 0:
print "No articles found"
else:
for pub in pubs.keys():
print pub + ' ' + pubs[pub]
但是遍历文档树的循环有问题,因为上面的代码导致:
10.1177/0748233717726877 [Influence of Four Kinds of PPCPs on Micronucleus Rate of the Root-Tip Cells of Vicia-faba and Garlic]. 10.1016/j.crvi.2015.02.001 [Influence of Four Kinds of PPCPs on Micronucleus Rate of the Root-Tip Cells of Vicia-faba and Garlic].
也就是说,我得到了正确的 DOI,但只是上一篇文章标题的副本,没有 DOI!
正确的输出应该是:
10.1177/0748233717726877 Malathion and dithane induce DNA damage in Vicia faba. 10.1016/j.crvi.2015.02.001 Impact of dual inoculation with Rhizobium and PGPR on growth and antioxidant status of Vicia faba L. under copper stress.
任何人都可以向我提供一些解决这个烦人问题的提示吗?
这从根本上是错误的:
for elem in xmldoc.iter(tag='ArticleTitle'): # <-- *ALL* <ArticleTitle> elements
...
for subelem in xmldoc.iter(tag='ArticleId'): # <-- *ALL* <ArticleId> elements
...
在 ElementTree 中没有读心术,它只选择与您<ArticleId>
最后一次看到的相关联的内容<ArticleTitle>
,因此您找到的任何与该代码相关的内容实际上都不会相关。
围绕实际的 XML 文档构建您的代码(“for each PubmedArticle...”)并使用相对搜索:
pubs = []
for pubmedArticle in xmldoc.iter(tag='PubmedArticle'):
# relative search within this <PubmedArticle>
articleTitle = pubmedArticle.find('./MedlineCitation/Article/ArticleTitle')
# always verify that there are actual results for a search
if articleTitle == None:
title = "No article title found"
else:
title = articleTitle.text
for articleId in pubmedArticle.iterfind('./PubmedData//ArticleId'):
if articleId.get("IdType") == "doi":
pubs.append({"doi": articleId.text, "title": title})
我还建议制作一个字典列表,而不是单个字典。在您的以下代码中处理会更容易:
[
{'doi': '10.1177/0748233717726877', 'title': 'Malathion and dithane induce DNA damage in Vicia faba.'},
{'doi': '10.1016/j.crvi.2015.02.001', 'title': 'Impact of dual inoculation with Rhizobium and PGPR on growth and antioxidant status of Vicia faba L. under copper stress.'}
]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句