9

特定の KML ファイルから座標を抽出するために pyKML モジュールを使用しています。

私の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 を解析してその構造の座標を取得する正しい方法は何ですか?

4

1 に答える 1