首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试用pyKML解析KML文件并提取数据

尝试用pyKML解析KML文件并提取数据
EN

Stack Overflow用户
提问于 2021-06-22 11:52:11
回答 1查看 742关注 0票数 0
代码语言:javascript
复制
<kml>
<Document>
    <name>EP-1B-03</name>
    <Style id="narda">
        <LineStyle>
            <color>ffff0000</color>
            <width>3</width>
        </LineStyle>
    </Style>
    <Folder>
        <name>WIDEBAND [1]</name>
        <visibility>0</visibility>
        <Folder>
            <name>[0 V/m &lt; X ≤ 0.15 V/m]</name>
            <visibility>0</visibility>
            <Placemark>
                <name>Value WIDEBAND: Low V/m</name>
                <styleUrl>#lvl_0_1</styleUrl>
                <visibility>0</visibility>
                <description><![CDATA[<table><tr><th>Date and time: </th><td>05/02/20 09:32:28</td></tr><tr><th>Temperature: </th><td>23 C° </td></tr><tr><th>Relative humidity: </th><td>23 % </td></tr><tr><th>Battery: </th><td>3.09 V </td></tr><tr><th>Speed: </th><td>4 km/h </td></tr><tr><th>Acceleration x: </th><td>-0.02 g </td></tr><tr><th>Acceleration y: </th><td>-0.01 g </td></tr><tr><th>Acceleration z: </th><td>0.00 g </td></tr></table>]]></description>
                <Point>
                    <coordinates>8.16007,44.0748641666667,0 </coordinates>
                </Point>
            </Placemark>
            <Placemark>
                <name>Value WIDEBAND: Low V/m</name>
                <styleUrl>#lvl_0_1</styleUrl>
                <visibility>0</visibility>
                <description><![CDATA[<table><tr><th>Date and time: </th><td>05/02/20 09:32:28</td></tr><tr><th>Temperature: </th><td>23 C° </td></tr><tr><th>Relative humidity: </th><td>23 % </td></tr><tr><th>Battery: </th><td>3.09 V </td></tr><tr><th>Speed: </th><td>4 km/h </td></tr><tr><th>Acceleration x: </th><td>-0.01 g </td></tr><tr><th>Acceleration y: </th><td>0.01 g </td></tr><tr><th>Acceleration z: </th><td>-0.02 g </td></tr></table>]]></description>
                <Point>
                    <coordinates>8.1600825,44.0748745833333,0 </coordinates>
                </Point>
            </Placemark>
            <Placemark>
                <name>Value WIDEBAND: Low V/m</name>
                <styleUrl>#lvl_0_1</styleUrl>
                <visibility>0</visibility>
                <description><![CDATA[<table><tr><th>Date and time: </th><td>05/02/20 09:32:28</td></tr><tr><th>Temperature: </th><td>23 C° </td></tr><tr><th>Relative humidity: </th><td>23 % </td></tr><tr><th>Battery: </th><td>3.09 V </td></tr><tr><th>Speed: </th><td>4 km/h </td></tr><tr><th>Acceleration x: </th><td>-0.01 g </td></tr><tr><th>Acceleration y: </th><td>0.01 g </td></tr><tr><th>Acceleration z: </th><td>-0.02 g </td></tr></table>]]></description>
                <Point>
                    <coordinates>8.160075,44.0748683333333,0 </coordinates>
                </Point>
            </Placemark>
    </Folder>

这是我的kml文件

这是我的密码:

代码语言:javascript
复制
from pykml import parser
from os import path
import pandas as pd
from lxml import etree
from pykml.factory import nsmap

kml_file = path.join( r'C:\Users\paliou\Documents\ep-1b-03.kml')
namespace = {"ns" : nsmap[None]}
with open(kml_file) as f:
    tree = parser.parse(f)
    root = tree.getroot()
    N = 0
    placemarks = {}
    for ch in root.Document.Folder.Folder.Placemark.getchildren():
        name = ch[0]
        print (name)
        for pl in ch.getchildren():
            print (pl)

为什么这不返回错误或数据?我想从“描述”选项卡中提取纬度、经纬度、名称和一些信息。我只打印了来自Placemark.name,Placemark.style,Placemark.visibility,Placemark.description的第一个标签数据

返回:值宽带:低V/m

#lvl__1

日期和时间: 05/02/20 09:32:28温度: 23℃相对湿度: 23 %电池: 3.09 V速度:4 km/h加速度x:-0.02 g加速度y:-0.01 g加速度z: 0.00 g

8.16007,44.0748641666667,0有更好的方法吗?在某个地方,我发现了一个像.findall(".//ns:Placemark",namesapces=namespace)这样的示例,有方法用标记检索数据吗?(因为我做不到)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-22 15:33:01

你做的比必要的复杂多了。

代码语言:javascript
复制
import xml.etree.ElementTree as ET
from pathlib import Path


kml_file_path = Path(r'68083057.xml')
tree = ET.parse(kml_file_path)
root = tree.getroot()
print(root.tag)  # kml
for placemark_node in root.findall("Document/Folder/Folder/Placemark"):
    print("Placemark =====")
    for child in placemark_node:
        print(" ", child.tag, child.text)

印上我

代码语言:javascript
复制
Placemark =====
  name Value WIDEBAND: Low V/m
  styleUrl #lvl_0_1
  visibility 0
  description <table> ...
  Point 
                    
Placemark =====
  name Value WIDEBAND: Low V/m
  styleUrl #lvl_0_1
  visibility 0
  description <table> ...
  Point 
                    
Placemark =====
  name Value WIDEBAND: Low V/m
  styleUrl #lvl_0_1
  visibility 0
  description <table> ...
  Point 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68083057

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档