我有一个XML文件,如下所示:
<tagA key1="val1" key2="val2" key3="val3">
<tagB.1 key1="val1" key2="val2" key3="val3"/>
<tagB.2 key1="val1" key2="val2" key3="val3"/>
<tagB.3 key1="val1" key2="val2" key3="val3"/>
<tagB.4 key1="val1" key2="val2" key3="val3"/>
<tagB.5 key1="val1" key2="val2" key3="val3"/>
</tagA>
我要做的是在key1
中提取key2
,key3
和tagB.x
的名称,并将它们放入列表中。这样我以后可以提取它的值。它应该能够处理更多或更少的元素,因为每个文件都是不同的。谢谢!
答案 0 :(得分:2)
您应该使用xml解析器:
xml="""
<tagA key1="val1" key2="val2" key3="val3">
<tagB.1 key1="val1" key2="val2" key3="val3"/>
<tagB.2 key1="val1" key2="val2" key3="val3"/>
<tagB.3 key1="val1" key2="val2" key3="val3"/>
<tagB.4 key1="val1" key2="val2" key3="val3"/>
<tagB.5 key1="val1" key2="val2" key3="val3"/>
</tagA>
"""
import xml.etree.ElementTree as ET
root = ET.fromstring(xml)
for child in root:
print child.tag, child.attrib.keys()
tagB.1 ['key3', 'key2', 'key1']
tagB.2 ['key3', 'key2', 'key1']
tagB.3 ['key3', 'key2', 'key1']
tagB.4 ['key3', 'key2', 'key1']
tagB.5 ['key3', 'key2', 'key1']