KML在Python中转换为字符串? [英] KML to string in Python?

查看:119
本文介绍了KML在Python中转换为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想下载一个KML文件,并在Python中将其特定元素打印为字符串.

I'd like to download a KML file and print a particular element of it as a string in Python.

谁能给我一个如何做到这一点的例子?

Could anyone give me an example of how to do this?

谢谢.

推荐答案

下载和解析我将留给其他答案.这是我如何检索KML文件中每个地标的描述和坐标.

Downloading and parsing I'll leave to the other answer. Here's how I retrieved the description and coordinates for each placemark in a KML file.

namespace = {'ns' : 'http://www.opengis.net/kml/2.2'}
placemarks = doc.xpath('//ns:Placemark', namespaces=namespace)

for placemark in placemarks :
    for description in placemark.xpath('.//ns:description', namespaces=namespace):
        descriptionText = description.text.strip()

    for coords in placemark.xpath('.//ns:coordinates', namespaces=namespace):
        coordinates = coords.text.strip()

    # Here you have the description and coordinates

描述和坐标的for循环也许可以重写,但是我还没有确切找到它.

The for loops for the description and coordinates could perhaps be rewritten, but I haven't found exactly how yet.

这篇关于KML在Python中转换为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆