问题描述
使用以下格式的文件(我尽可能缩小数据,因为它是一个非常大的文件)。这是未来 274 小时的 RAW 天气预报:
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<kml:kml xmlns:dwd="https://opendata.dwd.de/weather/lib/pointforecast_dwd_extension_V1_0.xsd" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:xal="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<kml:Document>
<kml:ExtendedData>
<dwd:ProductDeFinition>
... #Several more Values
<dwd:Forecasttimesteps>
<dwd:timestep>2021-07-22T10:00:00.000Z</dwd:timestep>
... #247 Values
</dwd:Forecasttimesteps>
</dwd:ProductDeFinition>
</kml:ExtendedData>
<kml:Placemark>
<kml:name>K2932</kml:name>
<kml:description>TUTTLINGEN</kml:description>
<kml:ExtendedData>
<dwd:Forecast dwd:elementName="PPPP">
<dwd:value>###247 PPPP-Values according to the Time above###.</dwd:value>
</dwd:Forecast>
<dwd:Forecast dwd:elementName="E_PPP">
<dwd:value>###247 E_PP-Values according to the Time above###</dwd:value>
</dwd:Forecast>
... ### Many more of there Elements
<dwd:Forecast dwd:elementName="RR1c">
<dwd:value>###247 RR1c-Values according to the Time above###</dwd:value>
</dwd:Forecast>
</kml:ExtendedData>
</kml:Placemark>
</kml:Document>
</kml:kml>
from xml.etree import ElementTree as etree
dwd = '{https://opendata.dwd.de/weather/lib/pointforecast_dwd_extension_V1_0.xsd}'
with open('./tmp/forecast/MOSMIX_L_2021072209_K2932.kml','rt') as f:
root = etree.parse(f)
root.find('.//{0}Forecast'.format(dwd)).attrib['{0}elementName'.format(dwd)]
输出[]:'PPPP'
我不知道如何深入了解“RR1c”以及“价值观”本身。有人吗?
解决方法
正如 Michael Kay 在评论中指出的那样,它可以在 XPath 的帮助下完成。下面是一个例子:
from xml.etree import ElementTree as etree
# declare namespaces map
ns = {
'dwd' : 'https://opendata.dwd.de/weather/lib/pointforecast_dwd_extension_V1_0.xsd'
}
with open('./tmp/forecast/MOSMIX_L_2021072209_K2932.kml','rt') as f:
root = etree.parse(f)
# retrieve value using xpath (returns list of elements matching criteria)
forecasts = root.findall('.//dwd:Forecast[@dwd:elementName="RR1c"]/dwd:value',ns)
# print text of the first element found
print(forecasts[0].text)