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

问题描述

<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文件

这是我的代码

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_0_1

0

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

8.16007,0 有没有更好的方法来做到这一点?在某处我发现了一个像 .findall(".//ns:Placemark",namesapces=namespace) 这样的样本 有没有办法用标签检索数据? (因为我做不到)

解决方法

你做的比必要的复杂。

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)

打印我

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 
                    

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...