鱼C论坛

 找回密码
 立即注册
查看: 1667|回复: 2

[已解决]求问大佬让用户自定义某个值

[复制链接]
发表于 2023-3-4 19:34:37 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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

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>
最佳答案
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')
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 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')
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-3-4 20:38:05 | 显示全部楼层

芜湖~谢谢您!你!是!我!滴!神!!!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-24 09:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表