I'm writing a script that checks a short xml file with the following data:
Example:
<manager>
<name="adi" lastName="kant">
<info ip="10.12.180.107" platform="linux" user="root" password="dangerous"/>
</name>
<name="dino" lastName="kant">
<info ip="10.12.180.108" platform="linux" user="root" password="dangerous"/>
</name>
</manager>
I'm trying to create a python script that will look into this xml file and remove the selected name information.
Example:
removeData(xmlFile)
print xmlFile
<manager>
<name="dino" lastName="kant">
<info ip="10.12.180.108" platform="linux" user="root" password="dangerous"/>
</name>
</manager>
The only solution I could come with was to read the file up to the name I wish to remove and then append that into one list, then read the file from the name after the name I wish to remove and append that to another list, combine those two lists and print that into my file.
Example:
h = open("/home/service/chimera/array_cert/test.txt", "r")
output = []
lines = h.readlines()
for line in lines:
if '<name="adi"' in line:
break
output.append(line)
i = 0
for line in lines:
i+=1
if '<name="dino"' in line:
break
for line in lines[i:]:
output.append(line)
h.close()
h = open("/home/service/chimera/array_cert/test.txt", "w")
h.truncate()
for line in output:
h.write(line)
But this seems needlessly complex. Is there a simpler way to do this?
Also I'm using python 2.6 on a Linux system.