分析XML头

2024-05-19 00:44:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图解析出以下内容:

'Execute Query'
'Execute Query'
'Execute Query'
'2018-11-28 00:00'
'2018-11-28 23:59'

我的XML看起来像这样

<?xml version='1.0' standalone='yes'?><Report Type='SLA Report'
 SiteName='Execute Query'
 SLA_Name='Execute Query'
 SLA_Description='Execute Query'
 From='2018-11-28 00:00'
 Thru='2018-11-28 23:59' 
 obj_device='4500'
 locations='69,31,'
>
  <Objective Type='Availability'/>
  <Goal>99.93</Goal>
  <Actual>100.00</Actual>
  <Compliant>Yes</Compliant>
  <Errors>0</Errors>
</Report>

这是我正在测试的代码

root = ET.fromstring(xml_data)

for child in root:
    print(child.tag, child.attrib)


for sla in root.findall('Type'):
    goal = sla.find('Goal').text
    actual = sla.find('Actual').text
    compliant = sla.find('Compliant').text
    errors = sla.find('Errors').text
    checks = sla.find('Checks').text
    data=[goal,actual,compliant,errors,checks]
    df = pd.DataFrame(data)

    print(df)

我得到了“根”项目的罚款(目标,实际等),但我似乎不能得到的项目在顶部。如何获取前5项并将其加载到数据帧中?谢谢


Tags: textreportchildexecutedatatyperootfind

热门问题