[英]Pull Values From XML Using Python ElementTree
我需要從 XML 格式的報告中提取一些值。 XML 的結構相當復雜,我無法理解如何引用 XML 結構中的不同級別。
我已經導入了 ElementTree 並加載了源文件,我可以成功地從樹的上層提取值。 不幸的是,所有 ElementTree 教程都使用了非常簡單的示例,我無法弄清楚深入挖掘結構所需的語法,尤其是在鍵和值重復時。
例如,這里我正在檢索“Total Calls”值:
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>
遍歷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"))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.