You've been super helpful before and I really appreciate all of your help.
Basically, what I need to do is check if a line already exists in a file and if it doesn't add it to a certain spot.
So let's say I have this XML file named write_it.xml:
- Code: Select all
<EnclosingTag>
<Fierce name="Item1" separator="," src="myfile1.csv" />
<Fierce name="Item2" separator="," src="myfile2.csv" />
<Fierce name="Item3" separator="," src="myfile3.csv" />
<Fierce name="Item4" separator="," src="myfile4.csv" />
<Fierce name="Item5" separator="," src="myfile5.csv" />
<NotFierce Name="Item22">
</NotFierce>
</EnclosingTag>
What I want to be able to do is check if something like
<Fierce name="Item1" separator="," src="myfile1.csv" /> already exists in the file and if it doesn't, write it into the file. Basically, I will be running this script on a daily basis, but I only want it to add the necessary code when it doesn't already exist (most likely the very first day it is run). Every other time it runs, if the script notices that the line(s) already exists, I want it not to add anything.
Any help would be appreciated.
Here's the code I wrote for writing to the XML file. I've tested it and played around until I got it working properly. I just need your help in adding the correct "if" statement to check if the string already exists before adding it to the file. THANKS!!!
- Code: Select all
#==========WRITING OUT FILES=========##
filesrc = r'C:\Reports\write_it.xml'
f = open(filesrc)
lines = f.readlines()
print lines
f.close()
'''
This is where the if statement will need to go. Something like, if '<Fierece name="%s" separator="," src="%s" />'%('VARIABLE','mytestfile.csv') doesn't already exist in the file, please set it to lines[2]. Else, ignore it and move on with the script.
'''
lines[2] = '\n<Fierce name="%s" separator="," src="%s" />\n'%('VARIABLE','mytestfile.csv')
f = open(filesrc, 'w')
f.writelines(lines)
f.close()
print file(filesrc).read()

