我正在 Parsing 中作業,但遇到了一個我不知道如何解決的問題。
我有一個 XML 檔案(見下文),我只想獲取預設中的 a 的值,即 -1
<global>
<setting lid="diagnosticEcgSpeed" val="-1" pers="" res="" unit="mm/s">
<txt id="001041" description="" type="">Geschwindigkeit</txt>
<value lid="1" val="-1" text="50"/>
<value lid="2" val="-2" text="25"/>
<value lid="4" val="-4" text="12,5"/>
<!-- todo: only one value is needed -> use adult value -->
<preset i="-1" c="-1" a="-1" />
</setting>
到目前為止,我試過這段代碼:
import xml.etree.ElementTree as ET
tree = ET.parse('basics.xml')
root = tree.getroot()
x=root.find(".//*[@lid='diagnosticEcgSpeed']/preset").attrib
print(x)
我得到:
{'i': '-1', 'c': '-1', 'a': '-1'}
我需要在我的代碼中更改什么才能只獲得 a 的值而不是預設中的所有屬性?
uj5u.com熱心網友回復:
由于回傳值是字典本身,您可以嘗試
import xml.etree.ElementTree as ET
tree = ET.parse(r"C:\Users\????????????\Downloads\new downloads\temp\abc.xml")
root = tree.getroot()
x=root.find(".//*[@lid='diagnosticEcgSpeed']/preset").attrib['a']
print(x)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/365385.html
上一篇:分組元素無法正常作業
