0
我正在使用Python讀取XML METAR(Weather)數據。我可以讀取數據,並且還添加了錯誤檢查(僅適用於下面的visibility_statute_mi!)。下面是XML數據的例子:使用Python從XML文檔獲取多個子值
<METAR>
<raw_text>
FALE 201800Z VRB01KT 9999 FEW016 BKN028 23/22 Q1010 NOSIG
</raw_text>
<station_id>FALE</station_id>
<observation_time>2013-01-20T18:00:00Z</observation_time>
<temp_c>23.0</temp_c>
<dewpoint_c>22.0</dewpoint_c>
<wind_dir_degrees>0</wind_dir_degrees>
<wind_speed_kt>1</wind_speed_kt>
<altim_in_hg>29.822834</altim_in_hg>
<quality_control_flags>
<no_signal>TRUE</no_signal>
</quality_control_flags>
<sky_condition sky_cover="FEW" cloud_base_ft_agl="1600"/>
<sky_condition sky_cover="BKN" cloud_base_ft_agl="2800"/>
<flight_category>MVFR</flight_category>
<metar_type>METAR</metar_type>
</METAR>
這裏是我的Python 2.7的代碼分析數據:
# Output the XML in a HTML friendly manner
def outputHTML(xml):
# The get the METAR Data list
metar_data = xml.getElementsByTagName("data")
# Our return string
outputString = ""
# Cycled through the metar_data
for state in metar_data:
#Gets the stations and cycle through them
stations = state.getElementsByTagName("METAR")
for station in stations:
# Grab data from the station element
raw_text = station.getElementsByTagName("raw_text")[0].firstChild.data
station_id = station.getElementsByTagName("station_id")[0].firstChild.data
observation_time = station.getElementsByTagName('observation_time')[0].firstChild.data
temp_c = station.getElementsByTagName('temp_c')[0].firstChild.data
dewpoint_c = station.getElementsByTagName('dewpoint_c')[0].firstChild.data
wind_dir_degrees = station.getElementsByTagName('wind_dir_degrees')[0].firstChild.data
wind_speed_kt = station.getElementsByTagName('wind_speed_kt')[0].firstChild.data
visibility_statute_mi = station.getElementsByTagName('visibility_statute_mi')
if len(visibility_statute_mi) > 0:
visibility_statute_mi = visibility_statute_mi[0].firstChild.data
altim_in_hg = station.getElementsByTagName('altim_in_hg')[0].firstChild.data
metar_type = station.getElementsByTagName('metar_type')[0].firstChild.data
# Append the data onto the string
string = "<tr><td>" + str(station_id) + "</td><td>" + str(observation_time) + "</td><td>" + str(raw_text) + "</td><td>" + str(temp_c) + "</td><td>" + str(dewpoint_c) + "</td></tr>"
outputString+=string
# Output string
return outputString
如何閱讀sky_condition數據和循環得到sky_cover和cloud_base_ft_agl值?
我還需要檢查是否有任何天空狀況值,因爲通常沒有云層覆蓋,也沒有數據。
Andre
嗨,我已經嘗試使用此代碼。它使用不同的系統來讀取XML。我需要使用現有的xml.dom.minidom。該代碼正在工作,我只需要找到一種測試sky_condition的方法,然後閱讀sky_cover和cloud_base_ft_agl值 – andrebruton
OK,@andrebruton,我已經更新了答案 – kr1