Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python从XML文档获取多个子值_Python_Python 2.7 - Fatal编程技术网

使用Python从XML文档获取多个子值

使用Python从XML文档获取多个子值,python,python-2.7,Python,Python 2.7,我正在使用Python读取XML METAR(天气)数据。我可以读取数据,还添加了错误检查(仅适用于下面的可见性)。以下是XML数据的一个示例: <METAR> <raw_text> FALE 201800Z VRB01KT 9999 FEW016 BKN028 23/22 Q1010 NOSIG </raw_text> <station_id>FALE</station_id> <observation_ti

我正在使用Python读取XML METAR(天气)数据。我可以读取数据,还添加了错误检查(仅适用于下面的可见性)。以下是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>

FALE 201800Z VRB01KT 9999 FEW016 BKN028 23/22 Q1010 NOSIG
法勒
2013-01-20T18:00:00Z
23
22
0
1.
29.822834
真的
MVFR
梅塔尔
下面是我解析数据的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    
#以HTML友好的方式输出XML
def outputHTML(xml):
#获取METAR数据列表
metar_data=xml.getElementsByTagName(“数据”)
#我们的返回字符串
outputString=“”
#在metar_数据中循环
对于metar_数据中的状态:
#获取站点并循环浏览它们
stations=state.getElementsByTagName(“METAR”)
对于车站中的车站:
#从station元素获取数据
原始文本=station.getElementsByTagName(“原始文本”)[0]。firstChild.data
station_id=station.getElementsByTagName(“station_id”)[0]。firstChild.data
观测时间=station.getElementsByTagName(“观测时间”)[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
风速=station.getElementsByTagName('wind\u speed_kt')[0].firstChild.data
可见性\u规约\u mi=station.getElementsByTagName('可见性\u规约\u mi')
如果len(可见性)大于0:
可见性\法规\ mi=可见性\法规\ mi[0]。firstChild.data
altim_in_hg=station.getElementsByTagName('altim_in_hg')[0].firstChild.data
metar_类型=station.getElementsByTagName('metar_类型')[0]。firstChild.data
#将数据附加到字符串上
string=“”+str(站点id)+“+str(观测时间)+”+str(原始文本)+“+str(临时c)+”+str(露点c)+”
outputString+=字符串
#输出字符串
返回输出字符串
我如何读取天空条件数据并循环以获得天空覆盖和云层基础英尺agl值

我还需要检查是否有任何天空条件值,因为通常没有云层覆盖,也没有数据


Andre

我会将xml解析为一个树并查询它,例如:

import xml.etree.ElementTree as et

xmltext = """
<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>
"""
tree = et.fromstring(xmltext)

for sky_con in tree.iterfind('sky_condition'):
    print sky_con.attrib["cloud_base_ft_agl"]
    print sky_con.attrib.keys()

我会将xml解析为一棵树并查询它,例如:

import xml.etree.ElementTree as et

xmltext = """
<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>
"""
tree = et.fromstring(xmltext)

for sky_con in tree.iterfind('sky_condition'):
    print sky_con.attrib["cloud_base_ft_agl"]
    print sky_con.attrib.keys()

嗨,我试过使用这个代码。它使用不同的系统来读取XML。我需要使用现有的xml.dom.minidom。代码正在运行,我只需要找到一种测试天空条件的方法,然后读取天空覆盖和云层底部的agl值。我已经尝试使用此代码。它使用不同的系统来读取XML。我需要使用现有的xml.dom.minidom。代码正在运行,我只需要找到一种测试天空条件的方法,然后读取天空覆盖和云层底部英尺agl值