我有一個結構如下所示的 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/150
提交
取消