鱼C论坛

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

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

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

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

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

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

  1. rhop0_user = input("please input value of rhop0: ")
  2. print(type(rhop0_user))
  3. rhop0_user = int(rhop0_user)
  4. from xml.etree import ElementTree as ET
  5. f = open(r'E:\DualSPHysics_v5.0.5\DualSPHysics_v5.0\examples\inletoutlet\0_inlet+dem\Casein_Def.xml')
  6. xml_text = f.read()
  7. root = ET.fromstring(xml_text)
  8. f.close()
  9. rhop0 = root.find('casedef').find('constantsdef').find('rhop0')
  10. print(rhop0)
  11. root.attrib
  12. rhop0.set('value','rhop0_user')
  13. root.attrib
  14. definition = root.find('casedef').find('geometry').find('definition')
  15. print(definition)
  16. root.attrib
  17. definition.set('dp','0.05')
  18. tree = ET.ElementTree(root)
  19. tree.write("Casein_Def.xml",encoding= 'utf-8')
复制代码



xml文件部分内容如下:
  1. <case>
  2.     <casedef>
  3.         <constantsdef>
  4.             <lattice bound="1" fluid="1" />
  5.             <gravity x="0" y="0" z="-9.81" comment="Gravitational acceleration" units_comment="m/s^2" />
  6.             <rhop0 value="1200" comment="Reference density of the fluid" units_comment="kg/m^3" />
  7.             <hswl value="1" auto="false" comment="Maximum still water level to calculate speedofsound using coefsound" units_comment="metres (m)" />
  8.             <gamma value="7" comment="Polytropic constant for water used in the state equation" />
  9.             <speedsystem value="0" auto="true" comment="Maximum system speed (by default the dam-break propagation is used)" />
  10.             <coefsound value="20" comment="Coefficient to multiply speedsystem" />
  11.             <speedsound value="0" auto="true" comment="Speed of sound to use in the simulation (by default speedofsound=coefsound*speedsystem)" />
  12.             
  13.             <coefh value="0.866025" comment="Coefficient to calculate the smoothing length (h=coefh*sqrt(3*dp^2) in 3D)" />
  14.             <cflnumber value="0.4" comment="Coefficient to multiply dt" />
  15.         </constantsdef>
  16.         <mkconfig boundcount="240" fluidcount="10" />
  17.         <geometry>
  18.             <definition dp="0.05" units_comment="metres (m)">
  19.                 <pointmin x="-5" y="-5" z="-5" />
  20.                 <pointmax x="10" y="20" z="20" />
  21.             </definition>
复制代码

最佳答案
2023-3-4 20:16:55
  1. rhop0_user = input("please input value of rhop0: ")
  2. print(type(rhop0_user))
  3. # rhop0_user = int(rhop0_user)                    不要转成整数   
  4. from xml.etree import ElementTree as ET
  5. f = open(r'Casein_Def.xml')
  6. xml_text = f.read()
  7. root = ET.fromstring(xml_text)
  8. f.close()
  9. rhop0 = root.find('casedef').find('constantsdef').find('rhop0')
  10. print(rhop0)
  11. root.attrib
  12. rhop0.set('value', rhop0_user)                    # rhop0_user 不要加引号
  13. root.attrib
  14. definition = root.find('casedef').find('geometry').find('definition')
  15. print(definition)
  16. root.attrib
  17. definition.set('dp','0.05')
  18. tree = ET.ElementTree(root)
  19. tree.write("Casein_Def.xml",encoding= 'utf-8')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-3-4 20:16:55 | 显示全部楼层    本楼为最佳答案   
  1. rhop0_user = input("please input value of rhop0: ")
  2. print(type(rhop0_user))
  3. # rhop0_user = int(rhop0_user)                    不要转成整数   
  4. from xml.etree import ElementTree as ET
  5. f = open(r'Casein_Def.xml')
  6. xml_text = f.read()
  7. root = ET.fromstring(xml_text)
  8. f.close()
  9. rhop0 = root.find('casedef').find('constantsdef').find('rhop0')
  10. print(rhop0)
  11. root.attrib
  12. rhop0.set('value', rhop0_user)                    # rhop0_user 不要加引号
  13. root.attrib
  14. definition = root.find('casedef').find('geometry').find('definition')
  15. print(definition)
  16. root.attrib
  17. definition.set('dp','0.05')
  18. tree = ET.ElementTree(root)
  19. tree.write("Casein_Def.xml",encoding= 'utf-8')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

芜湖~谢谢您!你!是!我!滴!神!!!!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-28 02:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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