两种方法:
old=open("test.xml")
lines=old.readlines()
a=""
i=1
newlines=[]
for line in lines:
if a in line:
line=line.replace(a,"a"+str(i)+"for a")
i=i+1
newlines.append(line)
for line in newlines:
print line
new=open("newtest.xml","w")
new.writelines(newlines)
new.close()
old.close()
结果:
import xml.dom.minidom
oldxmlfile=open("test.xml")
oldxml=oldxmlfile.read()
oldxmlfile.close()
doc = xml.dom.minidom.parseString(oldxml)
index=1
for node in doc.getElementsByTagName("a"):
node.tagName="a"+str(index)
index=index+1
newxml=doc.toprettyxml()
xmlfile=open("newxml2.xml","w")
xmlfile.write(newxml);
xmlfile.close()
lz是好人。