python改xml文档属性值遇到的一点小问题
xml文档的部分截图:
要改选中行timemax的值,我写的代码如下:
TimeMax_user = self.posCtrl3.GetValue()
TimeMax = root.find('execution').find('parameters').find('parameter').find('[@comment="Time of simulation"]')
print(TimeMax)
root.attrib
TimeMax.set('value',TimeMax_user)
报错提示是:'NoneType' object has no attribute 'set'
改了几次都是提示这个,求指教
该回答引用GPTᴼᴾᴱᴺᴬᴵ
您的代码中似乎有一些错误。下面是我为您提供的代码示例,其中包括将xml文档中的“lrimeMax”参数值更改为用户输入值的部分:
import xml.etree.ElementTree as ET
# 解析xml文件
tree = ET.parse('file.xml')
root = tree.getroot()
# 获取要更改的参数
time_max = root.find('.//parameter[@comment="Time of simulation"]')
# 获取用户输入值
time_max_user = input('Enter new value for Time of simulation: ')
# 更改参数值
time_max.set('value', time_max_user)
# 保存更改
tree.write('file.xml')
您可以将此代码中的“file.xml”替换为您要更改的xml文件的名称,并将用户输入的值存储在“time_max_user”变量中。然后,您可以使用“set()”方法更改“lrimeMax”参数的值,并使用“write()”方法保存更改。
不知道你这个问题是否已经解决, 如果还没有解决的话: