【发布时间】:2019-05-26 03:12:29
【问题描述】:
修改xml配置文件的属性时,属性的顺序会发生变化。
Before modifying xml: <connection user="testing" intervalInSeconds="50" versionUpdates="10" />
After modifying xml: <connection intervalInSeconds="50" user="testing" versionUpdates="10" />
如何在 python 3 中保持属性的顺序?
代码:
import xml.etree.ElementTree as ET
tree = ET.parse('file path')
root = tree.getroot()
# modifying an attribute
for elem in root.iter('connection'):
elem.set('versionUpdates', '10')
tree.write('file path', encoding="UTF-8", xml_declaration = True)
【问题讨论】:
标签: xml python-3.x xml-parsing