我正在使用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并获取该结构处的坐标的正确方法是什么? 最佳答案
在上面的示例中,root = parser.parse(fileobject).getroot()
在文件内容上调用parse()作为从上一行的fromstring()函数返回的字符串。
有两种使用pyKML解析KML文件的方法:
1:使用parse.parse()解析文件。
from pykml import parser
with open('MapSource.kml', 'r') as f:
root = parser.parse(f).getroot()
print(root.Document.Placemark.Point.coordinates)
2:使用parse.parsestring()解析字符串内容。from pykml import parser
root = parser.fromstring(open('MapSource.kml', 'rb').read())
print(root.Document.Placemark.Point.coordinates)
如果KML文件的第一行使用非UTF8编码,则XML序言 header 作为第一行,并尝试以二进制格式读取以'r'作为文本而以'rb'作为文本的文件,则方法#2可能会失败。注意如果在KML文档中未正确指定编码,解析可能会失败。由于名称和说明中使用国际和图形字符,因此在下面的示例中使用了ISO-8859-1编码。省略编码或使用““UTF-8”将使其成为无效的XML文件。
<?xml version="1.0" encoding="ISO-8859-1"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<Placemark>
<name>Río Grande</name>
<description>
Location: 18° 22′ 49″ N, 65° 49′ 53″ W
</description>
...
关于python - 使用pyKML解析KML文档,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26074069/