1

我一直试图自己解决这个问题,但我被困住了。我想将一个空文件夹添加到现有 KML 文件中的现有文件夹中。这是我到目前为止所拥有的,当我打开文件时,没有名为“test”的新文件夹。

import pykml
from pykml.factory import KML_ElementMaker as KML
from pykml import parser

x = KML.Folder(KML.name("test"))

with open("Scratch Paper.kml") as f:
    doc = parser.parse(f).getroot()
    a = doc.Document.Folder.Folder[0]
    a.append(x)
f.close()
4

1 回答 1

1

所以,结果比我想象的要容易得多......我仍在努力学习好的做法,但是以读取模式打开文件,复制我想要的数据,然后以写入模式重新打开似乎完成了我想要的是。

如果其他人看到更好的方法来做到这一点,请告诉我!谢谢!

import lxml
from lxml import etree
import pykml
from pykml.factory import KML_ElementMaker as KML
from pykml import parser

x = KML.Folder(KML.name("meow"))

with open("Scratch Paper.kml", "r+") as f:
    doc = parser.parse(f).getroot()
    print doc.Document.Folder.Folder[3].name
    a = doc.Document.Folder[0]
    a.append(x)
    finished = (etree.tostring(doc, pretty_print=True))

with open("Scratch Paper.kml", "w+") as f:
    f.write(finished)

print "Done!"
于 2013-12-18T21:48:53.627 回答