尝试使用 pyKML 解析 KML 文件并提取数据
Trying to parse KML file with pyKML and extract data
<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 < 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)
为什么这个 return 没有错误或数据?
我想从描述选项卡中提取纬度、经度、名称和一些信息。
我只是让它只打印来自 Placemark.name、Placemark.style、Placemark.visibility、Placemark.description
的第一个标签数据
return:
值宽带:低 V/m
#lvl_0_1
0
日期和时间:05/02/20 09:32:28Temperature:23 C° 相对湿度:23% 电池:3.09 V 速度:4 km/h 加速度 x:-0.02 g 加速度 y:-0.01 g 加速度 z: 0.00克
8.16007,44.0748641666667,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
<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 < 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)
为什么这个 return 没有错误或数据? 我想从描述选项卡中提取纬度、经度、名称和一些信息。 我只是让它只打印来自 Placemark.name、Placemark.style、Placemark.visibility、Placemark.description
的第一个标签数据return: 值宽带:低 V/m
#lvl_0_1
0
日期和时间:05/02/20 09:32:28Temperature:23 C° 相对湿度:23% 电池:3.09 V 速度:4 km/h 加速度 x:-0.02 g 加速度 y:-0.01 g 加速度 z: 0.00克8.16007,44.0748641666667,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