我正在使用 pyKML 模块从给定的 KML 文件中提取坐标。
我的Python代码如下:
from pykml import parser
fileobject = parser.fromstring(open('MapSource.kml', 'r').read())
root = parser.parse(fileobject).getroot()
print(xml.Document.Placemark.Point.coordinates)
但是,在运行此程序时,我收到以下错误:
ValueError: Unicode strings with encoding declaration are not supported. Please use bytes input or XML fragments without declaration.
寻找解决方案,我遇到了这个解决方案http://twigstechtips.blogspot.in/2013/06/python-lxml-strings-with-encoding.html从我尝试过的地方(我不确定是正确的方法):
from pykml import parser
from lxml import etree
from os import path
kml_file = open('MapSource.kml', 'r')
parser = etree.XMLParser(recover=True)
xml = etree.fromstring(kml_file, parser)
print(xml.Document.Placemark.Point.coordinates)
这给了我ValueError: can only parse strings
. 我解析 KML 并获取该结构的坐标的正确方法是什么?