Python, problems modifying a text file, searching for terms
I posted a question yesterday here: Finding and adding to a .kml file using python
I have read a bunch of tutorial and now have a better understanding of python and that is good. Yet I still can't seem to get my script right. I know I am so very close. Basically I want to add a bunch of jpg's to a .kml file, which is basically .xml in google earth. I want my program to find a google earth "placemarker" in the xml file called: TO-XXX
where XXX matches the TO-XXX.jpg. I already have a folder with a bunch of .jpgs whose filename matches the name of each placemarker. I need the program to find the
<name> (for example <name>TO-101</name>)
and add a line right below the name line with a:
<description> <img src=TO-101.jpg></description>.
So, I have the code written, but I just can't seem to get it to find the . Which is always written:
"\t\t\t<name>TO-XXX</name>\n".
So, here is the code:
import os
infile = 'TO-Hand-Holes2.kml' # the file I am reading
outfile = 'TO-Hand-Holes-Output.kml' # the file I plan to write to, using print for now
images = os.listdir("./images") # the images folder, all image names match names
source = open(infile, 'r')
target = open(outfile, 'w')
x = 0 #an incrementer
i = 0 # an incrementer
readxml = source.readline
while x < 20000: #There are about 17,000 lines in the .kml file
readxml = source.readline()
while i < len(images):
word = images[i]
if readxml == "\t\t\t<name>%s</name>\n" % word[:6]: #!!!!!!!!! the problem is here
print readxml #output the <name>
print word[:6] #output the <description>
hi开发者_开发百科t = 'true'
i = i + 1
else:
hit = 'false'
#print "test%s" % word[:6]
i = i + 1
if hit == 'false':
print ("%s") % readxml
x = x + 1
I just can't seem to get it to recognize the lines. Any suggestions?
Because indentation is syntax in python you really need to be careful about where things are located. This may come closer. It's not 100% complete, but it will point you in the right direction:
with open(infile) as fhi, open(outfile, 'w') as fho:
for line in fhi:
if line == 'myMatchString':
fho.write(line.replace('this', 'that'))
That uses the multiple file syntax for the with
statement, which was introduced in 2.7. Prior to 2.7 you'll have to nest a second with
to get the second file.
This would be better:
import os
infile = 'TO-Hand-Holes2.kml' # the file I am reading
outfile = 'TO-Hand-Holes-Output.kml' # the file I plan to write to, using print for now
images = os.listdir("./images") # the images folder, all image names match names
with open(infile, 'r') as source:
with open(outfile, 'w') as target:
for readxml in source:
for word in images:
hit = readxml == "\t\t\t<name>%s</name>\n" % word[:6]
if hit: #!!!!!!!!! the problem is here
print readxml #output the <name>
print >> target, word[:6] #output the <description>
I made some changes but I don't have the files to test it on. I made some Python-related changes for your learning purposes. I think you should test if the info you want is in the string rather than checking for equivalence. If you want to check for equivalence, you should use line.strip() because it will contain tabs, newlines, etc that you might not be accounting for (and you don't want to explicitly account for, tbh).
import os
infile = 'TO-Hand-Holes2.kml' # the file I am reading
outfile = 'TO-Hand-Holes-Output.kml' # the file I plan to write to, using print for now
images = os.listdir("./images") # the images folder, all image names match names
source = open(infile, 'r')
target = open(outfile, 'w')
for line in source.readlines(): #read all of the source lines into a list and iterate over them
for image in images: #you can iterate over a list like this
word = image[:6] #i moved the list slicing here so you only have to do it once
if "<name>" in line and word in line: #!!!!!!!!! the problem is here
print line #output the <name>
print word #output the <description>
hit = True #use the built-in Python boolean type
else:
hit = False
#print "test%s" % word[:6]
target.write(line)
if hit:
target.write("<description> <img src={0}></description>\n".format(image)
source.close()
target.close()
精彩评论