python parse conditional xml value
Question: Here is my XML file:

    
    
    <METAR>
    <wind_dir_degrees>210</wind_dir_degrees>
    <wind_speed_kt>14</wind_speed_kt>
    <wind_gust_kt>22</wind_gust_kt>
    </METAR>
    

Here is my script to parse the wind direction and speed. However, the wind
gust is a conditional value and doesn't always appear in my xml file. I'd like
to show the value if it does exist and nothing if it doesn't.

    
    
    import xml.etree.ElementTree as ET
    from urllib import urlopen
    
    link = urlopen('xml file')
    
    tree = ET.parse(link)
    root = tree.getroot()
    
    data = root.findall('data/METAR')
    for metar in data:
        print metar.find('wind_dir').text
    

I tried something like this but get errors

    
    
    data = root.findall('wind_gust_kt')
    for metar in data:
            if metar.find((wind_gust_kt') > 0:
               print "Wind Gust: ", metar.find('wind_gust_kt').text
    


Answer: You can use `findtext` with a default value of `''`, eg:

    
    
    print "Wind Gust: ", meta.findtext('wind_gust_kt', '')
    

