我将使用pyKML模块从给定的KML文件中提取坐标。
我的Python代码如下:
然而,运行此代码时,我遇到了以下错误:
我的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并获取该结构中的坐标?