gpt4 book ai didi

python - 如何将此 XML 解析为 python 数组或字典?

转载 作者:数据小太阳 更新时间:2023-10-29 02:33:22 25 4
gpt4 key购买 nike

这是我要解析的 XML 片段:

<DSMs>
<DSM class="ACE" order="320"/>
<DSM class="ACS" order="1900"/>
<DSM class="Aironet" order="1050"/>
<DSM class="Apache" order="4700"/>
<DSM class="AppSecDbProtect" order="1477"/>
<DSM class="ArborNetworksPravail" order="1554">
<Thresholds>
<Threshold name="MinNumEvents" value="5"/>
<Threshold name="AbandonAfterSuccessiveFailures" value="3"/>
</Thresholds>
<Templates>
<Template name="DeviceName" value="Arbor Networks Pravail @ $$SOURCE_ADDRESS$$"/>
</Templates>
</DSM>
<DSM class="ARN" order="2000"/>
<DSM class="ArpeggioSIFTIT" order="1553"/>
<DSM class="ArubaClearPass" order="545">
<Thresholds>
<Threshold name="MinNumEvents" value="5"/>
<Threshold name="AbandonAfterSuccessiveFailures" value="3"/>
</Thresholds>
<Templates>
<Template name="DeviceName" value="Aruba ClearPass Policy Manager @ $$SOURCE_ADDRESS$$"/>
<Template name="DeviceDescription" value="Aruba ClearPass Policy Manager Device"/>
</Templates>
</DSM>
</DSMs>

到目前为止我做了什么(部分代码):

ta_dsms = []
for level1 in root:
if level1.tag == 'DSMs':
for level2 in level1:
ta_dsm = level2.attrib
ta_dsms.append(ta_dsm)
print ta_dsms

ta_dsms 的当前输出如下:

 [{'class': 'ACE', 'order': '320'}, 
{'class': 'ACS', 'order': '1900'},
...]

我的问题是获取阈值和模板信息并将它们添加到我的数组中的优雅方法是什么。只有一些 DSM 有 child 。我整天都被困在这个问题上。谢谢你救了我的命!

最佳答案

这能得到你想要的吗?

import xml.etree.ElementTree as ET
tree = ET.parse('data.xml')
root = tree.getroot()

ta_dsms = []
for level1 in root:
d = {}
if level1.tag == 'DSM':
for k,v in level1.attrib.items():
d[k] = v

for level2 in level1:
threshold_list = []
if level2.tag == "Thresholds":
for c in level2.getchildren():
threshold_dic = {}
for k,v in c.attrib.items():
threshold_dic[k] = v
threshold_list.append(threshold_dic)
d["Thresholds"] = threshold_list
template_list = []
if level2.tag == "Templates":
for c in level2.getchildren():
template_dic = {}
for k,v in c.attrib.items():
template_dic[k] = v
template_list.append(template_dic)
d["Templates"] = template_list
ta_dsms.append(d)


print(ta_dsms)

结果是:

[  
{
"class":"ACE",
"order":"320"
},
{
"class":"ACS",
"order":"1900"
},
{
"class":"Aironet",
"order":"1050"
},
{
"class":"Apache",
"order":"4700"
},
{
"class":"AppSecDbProtect",
"order":"1477"
},
{
"class":"ArborNetworksPravail",
"Thresholds":[
{
"name":"MinNumEvents",
"value":"5"
},
{
"name":"AbandonAfterSuccessiveFailures",
"value":"3"
}
],
"Templates":[
{
"name":"DeviceName",
"value":"Arbor Networks Pravail @ $$SOURCE_ADDRESS$$"
}
],
"order":"1554"
},
{
"class":"ARN",
"order":"2000"
},
{
"class":"ArpeggioSIFTIT",
"order":"1553"
},
{
"class":"ArubaClearPass",
"Thresholds":[
{
"name":"MinNumEvents",
"value":"5"
},
{
"name":"AbandonAfterSuccessiveFailures",
"value":"3"
}
],
"Templates":[
{
"name":"DeviceName",
"value":"Aruba ClearPass Policy Manager @ $$SOURCE_ADDRESS$$"
},
{
"name":"DeviceDescription",
"value":"Aruba ClearPass Policy Manager Device"
}
],
"order":"545"
}
]

关于python - 如何将此 XML 解析为 python 数组或字典?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48632346/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com