我正在尝试更改 xml 文档中的许多值。我尝试了一些不同的东西,但它们似乎没有改变任何东西,但随着修改时间的变化,它们似乎确实可以访问文件,但值没有。
from xml.etree import ElementTree as et
import os
import xml
path = os.path.expanduser(r'~\AppData\Roaming\etc\etc\somefile.xml')
et = et.parse(path)
for name in et.findall('name'):
if name == 'sometext1':
name.text = "sometext2"
et.write(path)
其次尝试了这个,但我得到了一个 AttributeError: 'str' object has no attribute 'text'
with open(path,'r+') as f:
tree = et.parse(f)
for node in tree.iter('favourite'):
name = node.attrib.get('name')
if name == 'sometext1':
name.text = "sometext2"
tree.write(path)
谁能告诉我哪里出错了