python - Changing Values in a xml file -


i trying change numerous values in xml document. have tried couple different things dont seem change seem access file modification time changes value doesnt.

from xml.etree import elementtree et import os import xml  path = os.path.expanduser(r'~\appdata\roaming\etc\etc\somefile.xml') et = et.parse(path)  name in et.findall('name'):     if name == 'sometext1':         name.text = "sometext2" et.write(path) 

and secondly tried attributeerror: 'str' object has no attribute 'text'

with open(path,'r+') f: tree = et.parse(f)  node in tree.iter('favourite'): name = node.attrib.get('name')  if name == 'sometext1':     name.text = "sometext2" tree.write(path) 

could advise ive gone wrong

  • the line

    et = et.parse(path) 

    uses et module on right , variable name on left. after point, not possible (or @ least overly hard) access elementtree module. disambiguate et. let, say, et module, , tree elementtree.

  • in loop, name element, comparing name string false. instead use

    name.text == 'sometext1' 

from xml.etree import elementtree et import os  path = os.path.expanduser(r'~\appdata\roaming\etc\etc\somefile.xml') tree = et.parse(path)  name in tree.findall('name'):     if name.text == 'sometext1':         name.text = "sometext2"         print(name) # debugging tree.write(path) 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -