123lqqq 发表于 2023-3-4 19:34:37

求问大佬让用户自定义某个值

这波属于赶鸭子上架了{:5_104:} 我需要让用户自己输入他要的值,然后把xml文件中rhop0的值改成他需要的
我写的代码是这样的,明显不对,但是我也不知道怎么改{:5_100:}

rhop0_user = input("please input value of rhop0: ")
print(type(rhop0_user))
rhop0_user = int(rhop0_user)
from xml.etree import ElementTree as ET
f = open(r'E:\DualSPHysics_v5.0.5\DualSPHysics_v5.0\examples\inletoutlet\0_inlet+dem\Casein_Def.xml')
xml_text = f.read()
root = ET.fromstring(xml_text)
f.close()
rhop0 = root.find('casedef').find('constantsdef').find('rhop0')
print(rhop0)
root.attrib
rhop0.set('value','rhop0_user')
root.attrib
definition = root.find('casedef').find('geometry').find('definition')
print(definition)
root.attrib
definition.set('dp','0.05')
tree = ET.ElementTree(root)
tree.write("Casein_Def.xml",encoding= 'utf-8')


xml文件部分内容如下:
<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="1200" comment="Reference density of the fluid" units_comment="kg/m^3" />
            <hswl value="1" auto="false" comment="Maximum still water level to calculate speedofsound using coefsound" units_comment="metres (m)" />
            <gamma value="7" comment="Polytropic constant for water used in the state equation" />
            <speedsystem value="0" auto="true" comment="Maximum system speed (by default the dam-break propagation is used)" />
            <coefsound value="20" comment="Coefficient to multiply speedsystem" />
            <speedsound value="0" auto="true" comment="Speed of sound to use in the simulation (by default speedofsound=coefsound*speedsystem)" />
            
            <coefh value="0.866025" comment="Coefficient to calculate the smoothing length (h=coefh*sqrt(3*dp^2) in 3D)" />
            <cflnumber value="0.4" comment="Coefficient to multiply dt" />
      </constantsdef>
      <mkconfig boundcount="240" fluidcount="10" />
      <geometry>
            <definition dp="0.05" units_comment="metres (m)">
                <pointmin x="-5" y="-5" z="-5" />
                <pointmax x="10" y="20" z="20" />
            </definition>

isdkz 发表于 2023-3-4 20:16:55

rhop0_user = input("please input value of rhop0: ")
print(type(rhop0_user))
# rhop0_user = int(rhop0_user)                  不要转成整数   
from xml.etree import ElementTree as ET
f = open(r'Casein_Def.xml')
xml_text = f.read()
root = ET.fromstring(xml_text)
f.close()
rhop0 = root.find('casedef').find('constantsdef').find('rhop0')
print(rhop0)
root.attrib
rhop0.set('value', rhop0_user)                  # rhop0_user 不要加引号
root.attrib
definition = root.find('casedef').find('geometry').find('definition')
print(definition)
root.attrib
definition.set('dp','0.05')
tree = ET.ElementTree(root)
tree.write("Casein_Def.xml",encoding= 'utf-8')

123lqqq 发表于 2023-3-4 20:38:05

isdkz 发表于 2023-3-4 20:16


芜湖~谢谢您!你!是!我!滴!神!!!!
页: [1]
查看完整版本: 求问大佬让用户自定义某个值