使用 python 查找并添加到 .kml 文件 [英] Finding and adding to a .kml file using python

查看:37
本文介绍了使用 python 查找并添加到 .kml 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是一个很好的编码员.但我真的很想自动化我在工作中必须做的事情.基本上,我在谷歌地球中有一堆地标显示市政光纤网络的手孔.我有每个手孔的几张图片,我想将它们作为标记放置在 .kml 文件中.基本上,我想拥有它,所以当他们点击谷歌地球中的地标时,它会显示小描述气泡并显示每个手孔的图片以及其他一些重要信息.

I am not a very good coder. But I really want to automate something I have to do at work. Basically, I have a bunch of placemarkers in google earth showing hand-holes of a municipal fiber network. I have a few pictures of each hand-hole that I want to place as a tag in the .kml file. Basically, I want to have it so when they click on a placemarker in google earth, it brings up the little description bubble and shows the pictures for each hand-hole and probably some other vital info.

现在我将每个手孔都标记为 TO-.所以101号手孔有一个TO-101.而且我已经制作了一个脚本,该脚本已重命名所有图片以匹配名称.所以,如果手孔TO-101有3张图片,那就是TO-101-1.jpg、TO-101-2.jpg、TO-101-3.jpg.

Right now I have each hand hole labeled as either TO-. So hand-hole number 101 has a TO-101. And I have already made a script that has renamed all the pictures to match the name. So, if hand hole TO-101 has 3 pictures, they would be TO-101-1.jpg, TO-101-2.jpg, TO-101-3.jpg.

所以,基本上,我正在尝试制作一个读取 kml 文件的 python 脚本(我已经成功完成了).我需要它在 .kml 文件中找到相应的 TO-101 或任何handhole,在该行下方添加一行,内容如下:

So, basically, I am trying to make a python script that reads the kml file (which I have done succesfully). I need it to find the corresponding TO-101 or whichever handhole in the .kml file, add a line below that line that reads:

<description> <img src="TO-101-1.jpg"><img src="TO-101-2.jpg"></description>. 

需要为大约 5000 张图片和 2000 个左右的手孔执行此操作.

Need to do this for about 5000 pictures and 2000 or so hand-holes.

我已经尝试过:

import string, glob, sys, os, fileinput
f = open('./hand-holes.kml','r')

while f:
line = f.readline()
if line == "<name>TO-101</name>":
print line

我什至无法找到并打印该行.我知道该行在文件中,并且我知道路径是正确的.因为如果我注释掉 if line == "TO-101" 它会显示文件的内容.所以,如果我什至不能让它工作,我就没有机会让它其余部分工作.我曾尝试查看几段搜索和替换类型 python 示例代码.其中一些使用字符串库,我不确定是否需要使用.

And I can't even get it to find and print that line. I know the line is in the file, and I know the path is right. Because if I comment out the if line == "TO-101" it displays the contents of the file. So, if I can't even get that to work, there is no chance I am going to get the rest of it to work. I have tried looking at several pieces of search and replace type python example code. Some of it uses a string library, which I am not sure if I need to use.

谁能帮帮我?谢谢

肾病

推荐答案

我假设你的 if line == "TO-101" 行没有的原因't work 是因为它前面可能有空格.如果您将其更改为 line.strip() == ... 那么它可能会起作用.

I'm assuming the reason your if line == "<name>TO-101</name>" line doesn't work is because it probably has whitespace preceding it. If you changed it to line.strip() == ... then it would presumably work.

最好的办法是实现一个 XML 处理程序来读取现有文件、解析它并添加新部分.从 xml.sax

Your best bet would be to implement an XML handler to read in the existing file, parse it, and add the new section. Start maybe with xml.sax

这篇关于使用 python 查找并添加到 .kml 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆