Python,修改文本文件、搜索术语时出现问题

发布于 2024-11-28 03:37:16 字数 1743 浏览 3 评论 0原文

我昨天在这里发布了一个问题: 查找并添加到 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柠檬 2024-12-05 03:37:16

因为缩进是 Python 中的语法,所以你确实需要小心事物的位置。这可能会更接近。它不是 100% 完整,但它会为您指明正确的方向:

with open(infile) as fhi, open(outfile, 'w') as fho:
  for line in fhi:
    if line == 'myMatchString':
      fho.write(line.replace('this', 'that'))

它使用 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:

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.

何以心动 2024-12-05 03:37:16

这样会更好:

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>

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>
污味仙女 2024-12-05 03:37:16

我做了一些更改,但没有用于测试的文件。为了您的学习目的,我做了一些与 Python 相关的更改。我认为您应该测试您想要的信息是否在字符串中,而不是检查等效性。如果你想检查等价性,你应该使用 line.strip() ,因为它将包含你可能没有考虑到的制表符、换行符等(而且你不想明确地考虑,说实话)。

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()

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文