2 回答

TA貢獻1824條經(jīng)驗 獲得超8個贊
我看到你發(fā)現(xiàn)“'< ... />' 之間的內(nèi)容”是屬性。
迭代add元素并檢查key屬性值的另一種方法是檢查謂詞中的屬性值。
例子...
蟒蛇
import xml.etree.ElementTree as ET
tree = ET.parse("my_file_name")
root = tree.getroot()
root.find('appSettings/add[@key="updaterApplication"]').attrib["value"] = "Updater v4.4"
print(ET.tostring(root).decode())
輸出
<configuration>
<appSettings>
<add key="title" value="Donny" />
<add key="updaterApplication" value="Updater v4.4" />
</appSettings>
</configuration>

TA貢獻1798條經(jīng)驗 獲得超3個贊
沒關(guān)系...:
import xml.etree.ElementTree as ET
tree = ET.parse(my_file_name)
root = tree.getroot()
for elm in root.iter('add'):
if elm.attrib['key']=='updaterApplication':
elm.attrib['value'] = 'Updater v4.4'
print(elm.attrib)
添加回答
舉報