这就是事情:
<parent>
<child attribute1="100" attribute2="1,2"/>
<child attribute1="200" attribute2="1,2,5"/>
<child attribute1="300" attribute2="1,2,5,10"/>
</parent>
解析后我想看到的内容:
100 [1, 2]
200 [1, 2, 5]
300 [1, 2, 5, 10]
作为清单。
答案 0 :(得分:0)
简单地拆分属性值并将它们转换为每个元素的整数。
使用ElementTree:
from xml.etree import ElementTree
tree = ElementTree.fromstring(example)
for child in tree.findall('.//child'):
attribute1 = int(child.attrib['attribute1'])
attribute2 = [int(v) for v in child.attrib['attribute2'].split(',')]
print attribute1, attribute2
对于您的示例文档,打印出:
>>> for child in tree.findall('.//child'):
... attribute1 = int(child.attrib['attribute1'])
... attribute2 = [int(v) for v in child.attrib['attribute2'].split(',')]
... print attribute1, attribute2
...
100 [1, 2]
200 [1, 2, 5]
300 [1, 2, 5, 10]