马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
需要用python代码改一个xml文件里的数值,代码没报错,但是运行后rhop0 value的属性还是没改。
谢谢各位大佬,非常感谢
from xml.etree import ElementTree as ET
f = open(r'C:\Users\QQLiao\Desktop\ruanzhu.xml\casedef.xml')
xml_text = f.read()
root = ET.fromstring(xml_text)
f.close()
rhop0 = root.find('casedef').find('constantsdef').find('rhop0 value')
root.attrib
rhop0 =root.set('rhop0 value','1200')
root.attrib
tree = ET.ElementTree(root)
tree.write("new.xml",encoding= 'utf-8')# -*- coding: utf-8 -*-
下面xml文件部分内容
<?xml version="1.0" encoding="UTF-8" ?>
<case>
<casedef>
<constantsdef>
<lattice bound="1" fluid="1" />
<gravity x="0" y="0" z="-9.81" comment="Gravitational acceleration" units_comment="m/s^2" />
<rhop0 value="1000" comment="Reference density of the fluid" units_comment="kg/m^3" />
本帖最后由 isdkz 于 2023-3-2 16:28 编辑
from xml.etree import ElementTree as ET
f = open(r'C:\Users\QQLiao\Desktop\ruanzhu.xml\casedef.xml')
xml_text = f.read()
root = ET.fromstring(xml_text)
f.close()
rhop0 = root.find('casedef').find('constantsdef').find('rhop0') # 改了这里
root.attrib
rhop0.set('value','1200') # 改了这里
root.attrib
tree = ET.ElementTree(root)
tree.write("new.xml",encoding= 'utf-8')
|