亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 Python xml.etree.ElementTree 遍歷 XML 樹的問題

使用 Python xml.etree.ElementTree 遍歷 XML 樹的問題

aluckdog 2021-10-26 15:25:26
我有一個結構如下所示的 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>為了實現這一點,我使用了 xml.etree.ElementTree,如下所示:import xml.etree.ElementTree as ETxmldoc = 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] = titleif 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.任何人都可以向我提供一些解決這個煩人問題的提示嗎?
查看完整描述

目前暫無任何回答

  • 0 回答
  • 0 關注
  • 203 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號