0
<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,44.0748641666667,0 有没有更好的办法呢?我在某个地方找到了一个像 .findall(".//ns:Placemark",namesapces=namespace) 这样的样本有没有办法用标签检索数据?(因为我无法让它工作)

4

1 回答 1

0

你做的比必要的更复杂。

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 
                    
于 2021-06-22T15:33:01.930 回答