所以我有一个XML,它有一行:
<factor-apply>1.2</factor-apply>
And this line is in my XML twice.个
我想将案文for both lines改为:
<factor-apply>0.5</factor-apply>
我的代码如下:
import xml.etree.ElementTree as ET
xml_tree = ET.parse(TestXML.xml)
root = xml_tree.getroot()
root.find(".//{*}factor-apply").text = "0.5"
ET.tostring(root)
现在,这改变了我的XML中的第一个因素行,但没有改变第二个因素行.
我试着go 做
root.findall(".//{*}factor-apply").text = "0.5"
但我收到了错误消息
AttributeError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_5072\1499665917.py in <module>
3 xml_tree = ET.parse(TestXML.xml)
4 root = xml_tree.getroot()
----> 5 root.findall(".//{*}factor-apply").text = "0.5"
6 ET.tostring(root)
AttributeError: 'list' object has no attribute 'text'
我在想,如果Findall不起作用,我是不是必须做一个循环才能做到这一点? 如有任何建议,我们将不胜感激.
我希望能够对任何变量执行此操作.所以我有一条与出生日期相对应的线.但这行出现了10次,所以我想要更改所有10行,例如