2
我想了解如何使用Python(2.7)中的cElementTree iterparse检索XML属性值。我的XML是这样的:使用Python检索XML属性值iterparse
<root>
<record attr1="a" attr2="b" attr3="c" ... />
<record attr1="x" attr2="y" attr3="z" ... />
...
</root>
我的代码是这样的:
context = ET.iterparse(sys.stdin, events=('start','end'))
context = iter(context)
event, root = context.next()
for event, elem in context:
if event == 'end' and elem.tag == 'record':
# get the attributes!!
elem.clear()
root.clear()
我处理标准输入大数据。 我没有任何运气可以搞清楚这一点。有人可以告诉我(最佳?)如何做到这一点?
检索字典正确的方法是: elem.attrib不需要 开括号 – ealbert 2014-12-04 12:36:55