使用Python ElementTree从XML中提取值

布赖恩

我需要从XML格式的报表中提取一些值。XML的结构相当复杂,我无法理解如何引用XML结构中的不同级别。

我已经导入了ElementTree并加载了源文件,并且可以成功地从树的上层提取值。不幸的是,所有ElementTree教程都使用了非常简单的示例,我无法弄清楚深入研究结构所需的语法,尤其是在键和值重复的情况下。

例如,在这里我要检索“通话总数”值:

tree = ET.parse('Z:/VSCode/Python/CallStats/calls.xml')
root = tree.getroot()

#Get Total Calls
for totcalls in root.iter('TotalCalls'):
    print(totcalls.text)

但这会返回重复的值,因为TotalCalls标记在报表中两次出现在不同的级别<Overview><KeyFacts>并且我只想从<KeyFacts>级别读取值

请问从该<KeyFacts>级别检索TotalCalls值的语法是什么

这是源文件:

<?xml version="1.0" encoding="UTF-8"?><Report version="1">
    <ReportConfig>
        <Name>Test01</Name>
        <BeginDate>Fri Jan 29 05:00:00 EST 2021</BeginDate>
        <EndDate>Fri Jan 29 05:14:59 EST 2021</EndDate>
        <GenerationTime>Fri Jan 29 05:15:01 EST 2021</GenerationTime>
        <DataSource>Live Traffic</DataSource>
        <AggregationType>Summary</AggregationType>
        <ReportPeriod>15 Minutes</ReportPeriod>
        <Class name="Service">
            <ResGroup description="" type="">All (Individual)</ResGroup>
            <Service description="VOIP Service">VoIP</Service>
        </Class>
    </ReportConfig>
    <DataAvailable>true</DataAvailable>
    <Aggregation name="Summary">
        <Resource name="TestInstance">
            <Overview>
                <AverageServiceHealth>4.1</AverageServiceHealth>
                <ReportOutcomeHistogram>
                    <Fail>16</Fail>
                    <Busy>0</Busy>
                    <NoAnswer>0</NoAnswer>
                    <Answered>2323</Answered>
                </ReportOutcomeHistogram>
                <AverageLossPct>0.1</AverageLossPct>
                <AverageJitterMS>3.3458</AverageJitterMS>
                <AverageDelayMS>91.6</AverageDelayMS>
                <AverageDurationSecs>312.8512</AverageDurationSecs>
                <TotalCalls>565</TotalCalls>
                <TotalCallMinutes>12112.5552</TotalCallMinutes>
                <AnswerSeizureRatio>99.3</AnswerSeizureRatio>
                <NetworkEffectivenessRatio>99.3</NetworkEffectivenessRatio>
            </Overview>
            <ServiceQuality xaxis="MOS-CQ" title="VoIP Service Quality">
                <AverageServiceHealth>4.1</AverageServiceHealth>
                <MOSHistogram title="MOS-CQ">
                    <Bin lower="1.0" higher="3.099">7</Bin>
                    <Bin lower="3.1" higher="3.199">15</Bin>
                    <Bin lower="3.2" higher="3.299">3</Bin>
                    <Bin lower="3.3" higher="3.399">0</Bin>
                    <Bin lower="3.4" higher="3.499">5</Bin>
                    <Bin lower="3.5" higher="3.599">7</Bin>
                    <Bin lower="3.6" higher="3.699">9</Bin>
                    <Bin lower="3.7" higher="3.799">15</Bin>
                    <Bin lower="3.8" higher="3.899">27</Bin>
                    <Bin lower="3.9" higher="3.999">332</Bin>
                    <Bin lower="4.0" higher="4.099">472</Bin>
                    <Bin lower="4.1" higher="4.199">55</Bin>
                    <Bin lower="4.2" higher="4.299">1378</Bin>
                    <Bin lower="4.3" higher="4.399">0</Bin>
                    <Bin lower="4.4" higher="5.000">0</Bin>
                </MOSHistogram>
            </ServiceQuality>
            <KeyFacts>
                <NumberOfCallAttempts>565</NumberOfCallAttempts>
                <TotalCalls>565</TotalCalls>
                <TotalRecords>2339</TotalRecords>
                <TotalCallMinutes>12112.5552</TotalCallMinutes>
            </KeyFacts>
            <CallStatistics>
                <AnswerSeizureRatio>99.3</AnswerSeizureRatio>
                <NetworkEffectivenessRatio>99.3</NetworkEffectivenessRatio>
                <AverageCallDurationSec>312.9</AverageCallDurationSec>
                <NumberOfCallAttempts>565</NumberOfCallAttempts>
                <ReportedOutcomes>
                    <Answered>2323</Answered>
                    <Unanswered>0</Unanswered>
                    <Busy>0</Busy>
                    <ReportedFailures>16</ReportedFailures>
                    <Dropped>12</Dropped>
                    <OneWayMedia>4</OneWayMedia>
                    <NoMedia>0</NoMedia>
                    <NotFound>0</NotFound>
                    <Unauthorized>0</Unauthorized>
                    <ServerBusy>0</ServerBusy>
                    <ServerError>0</ServerError>
                    <BadSignaling>0</BadSignaling>
                </ReportedOutcomes>
                <NumberOfRegistrationFailures>0</NumberOfRegistrationFailures>
            </CallStatistics>
            <SLA>
                <SLAFailCount>0</SLAFailCount>
                <RecordCount>2339</RecordCount>
                <ObservedPassRate>100.000</ObservedPassRate>
            </SLA>
            <AlertsGenerated>
                <CriticalCount>0</CriticalCount>
                <WarnCount>0</WarnCount>
            </AlertsGenerated>
        </Resource>
    </Aggregation>
</Report>
z

遍历KeyFacts元素并获取其TotalCalls元素的文本值

import xml.etree.ElementTree as ET
 
tree = ET.parse('Z:/VSCode/Python/CallStats/calls.xml')
 
for kf in tree.iter('KeyFacts'):
    print(kf.findtext("TotalCalls"))

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章