Python,修改文本文件、搜索术语时出现问题
我昨天在这里发布了一个问题: 查找并添加到 .kml file using python
我已经阅读了很多教程,现在对 python 有了更好的理解,这很好。但我似乎仍然无法正确地编写我的脚本。我知道我非常接近。基本上我想将一堆 jpg 添加到 .kml 文件中,该文件基本上是 google Earth 中的 .xml 。我希望我的程序在 xml 文件中找到一个 google Earth“地标”,名为: TO-XXX,
其中 XXX 与 TO-XXX.jpg 匹配。我已经有一个包含一堆 .jpg 的文件夹,其文件名与每个地标的名称相匹配。我需要程序来查找
<name> (for example <name>TO-101</name>)
并在名称行正下方添加一行:
<description> <img src=TO-101.jpg></description>.
因此,我已经编写了代码,但我似乎无法让它找到 .总是这样写:
"\t\t\t<name>TO-XXX</name>\n".
所以,这是代码:
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>
hit = '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 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>
hit = '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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因为缩进是 Python 中的语法,所以你确实需要小心事物的位置。这可能会更接近。它不是 100% 完整,但它会为您指明正确的方向:
它使用
with
语句的多文件语法,该语法是在 2.7 中引入的。在 2.7 之前,您必须嵌套第二个with
才能获取第二个文件。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:
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 secondwith
to get the second file.这样会更好:
This would be better:
我做了一些更改,但没有用于测试的文件。为了您的学习目的,我做了一些与 Python 相关的更改。我认为您应该测试您想要的信息是否在字符串中,而不是检查等效性。如果你想检查等价性,你应该使用 line.strip() ,因为它将包含你可能没有考虑到的制表符、换行符等(而且你不想明确地考虑,说实话)。
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).