如何使用 Python elementTree 提取 xml 数据中的特定元素

How to extract particular elements in xml data using Python elementTree

我正在尝试从大型 XML 输出中提取一些数据,有人能帮我提取第 99 个百分位数的 TotalMilliseconds 吗?

我正在尝试使用 ElementTree 解析器在 Python 中执行此操作。

我目前正在尝试做这样的事情,它找到第 99 个百分位数,然后尝试从该级别的根部找到 TotalMilliseconds

但这 returns 没什么,在调试中我可以看到它进入了 99 子句,但我有点迷失了我从那里去的地方。

for item in root.findall('./TimeSpan/Latency/Bucket/Percentile'):
    if item.text == "99":
        totalMs = item.find('TotalMilliseconds').text
        print(totalMs)
<TimeSpan>
<Latency>
<Bucket>
<Percentile>96</Percentile>
<ReadMilliseconds>55.378</ReadMilliseconds>
<WriteMilliseconds>105.115</WriteMilliseconds>
<TotalMilliseconds>98.546</TotalMilliseconds>
</Bucket>
<Bucket>
<Percentile>97</Percentile>
<ReadMilliseconds>59.552</ReadMilliseconds>
<WriteMilliseconds>109.733</WriteMilliseconds>
<TotalMilliseconds>104.649</TotalMilliseconds>
</Bucket>
<Bucket>
<Percentile>98</Percentile>
<ReadMilliseconds>64.891</ReadMilliseconds>
<WriteMilliseconds>116.998</WriteMilliseconds>
<TotalMilliseconds>111.300</TotalMilliseconds>
</Bucket>
<Bucket>
<Percentile>99</Percentile>
<ReadMilliseconds>81.629</ReadMilliseconds>
<WriteMilliseconds>131.931</WriteMilliseconds>
<TotalMilliseconds>125.176</TotalMilliseconds>
</Bucket>
</Latency>
</TimeSpan>

见下文

import xml.etree.ElementTree as ET

data = """<?xml version="1.0" encoding="UTF-8"?>
<TimeSpan>
   <Latency>
      <Bucket>
         <Percentile>96</Percentile>
         <ReadMilliseconds>55.378</ReadMilliseconds>
         <WriteMilliseconds>105.115</WriteMilliseconds>
         <TotalMilliseconds>98.546</TotalMilliseconds>
      </Bucket>
      <Bucket>
         <Percentile>97</Percentile>
         <ReadMilliseconds>59.552</ReadMilliseconds>
         <WriteMilliseconds>109.733</WriteMilliseconds>
         <TotalMilliseconds>104.649</TotalMilliseconds>
      </Bucket>
      <Bucket>
         <Percentile>98</Percentile>
         <ReadMilliseconds>64.891</ReadMilliseconds>
         <WriteMilliseconds>116.998</WriteMilliseconds>
         <TotalMilliseconds>111.300</TotalMilliseconds>
      </Bucket>
      <Bucket>
         <Percentile>99</Percentile>
         <ReadMilliseconds>81.629</ReadMilliseconds>
         <WriteMilliseconds>131.931</WriteMilliseconds>
         <TotalMilliseconds>125.176</TotalMilliseconds>
      </Bucket>
   </Latency>
</TimeSpan>"""

root = ET.fromstring(data)
# data is a list to support the case of many 99 Percentile
data = [e.find('TotalMilliseconds').text for e in root.findall('.//Bucket') if e.find('Percentile').text == '99']
print(data)

输出

['125.176']

不是搜索和遍历 Percentile 元素,而是遍历 Bucket 元素。然后,对于每个 Bucket,找到并比较它的 Percentile 并像您已经做的那样继续:

import xml.etree.ElementTree as ET

xml = """
    YOUR XML
"""

tree = ET.fromstring(xml)

for item in tree.findall(".//Bucket"):
    if item.find("Percentile").text == "99":
        totalMs = item.find("TotalMilliseconds").text
        print(totalMs)

输出:

125.176