我有这个结构的文档:遍历XML文档
<?xml version="1.0" encoding="UTF-8"?>
<entries>
<entry>
<term>word_1</term>
<opinion source="data1" polarity="0.10" />
<opinion source="data2" polarity="0.4" />
</entry>
<entry>
<term>word_2</term>
<opinion source="data1" polarity="1.0" />
<opinion source="data2" polarity="-0.16666667" />
<opinion source="data3" polarity="0.004" />
</entry>
<entry>
<term>word_3</term>
<opinion source="data1" polarity="0.6" />
<opinion source="data2" polarity="0.0" />
</entry>
</entries>
我从来没有与xml
工作之前,它被证明是一种痛苦。我想提取文字,它们的极性和来源。理想情况下,从这个例子中,我将有三个以source
命名的字典(我知道它有很多不同的来源,所以手动命名字典不是问题),这将保留字key
和极性value
即,
data1 = {'word1':0.10, 'word2':1.0, 'word3':0.6}
data2 = {'word1':0.4, 'word2':-0.16666667, 'word3':0.0}
data3 = {'word2':0.004}
问题是,我真的不知道如何迭代这个结构。我可以遍历<term>
像这样:
import xml.etree.ElementTree as ET
tree = ET.parse('my.xml')
root = tree.getroot()
for term in root.iter('term'):
print term.text
Out:
word_1
word_2
word_3
但我不能获取到source
和polarity
项目。 任何帮助表示赞赏。谢谢。
根据[此XML验证程序](http://www.xmlvalidation.com/index.php?id=1&L=0),此文件无效。我也无法用'ET'解析它。 –
@ gr1zzlybe4r是的,抱歉,我复制了错误的剪辑。我编辑了文件。它现在应该工作。 – Zlo
很高兴你明白了:) –