Findnext Not found.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • akshay1991
    New Member
    • Apr 2015
    • 1

    Findnext Not found.

    Hello experts,

    i found the program on youtube and try to figure out the bus towards to north longitude and latitude near to dave using xml file but it showing me an error....
    lat = float(bus.findn ext('lat'))

    AttributeError: 'xml.etree.Elem entTree.Element ' object as no attribute 'findnext'


    Here's the code,
    Code:
    import urllib
    
    daves_latitude = 41.98062
    daves_longitude = -87.668452
    
    from xml.etree.ElementTree import parse
    doc = parse('rt22.xml')
    
    for bus in doc.findall('bus'):
        lat = float(bus.findnext('lat'))
        if lat > daves_latitude:
            direction = bus.findnext('d')
            if direction.startwith('North'):
                busid = bus.findnext('id')
                print (busid,lat)
    here is the xml file link.
    Get estimated arrival times for CTA buses or see them on a map.





    Thank you in advance..
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    As the traceback indicates, bus has no "findnext" method. It has a "findall" method which returns a list.
    Code:
    from xml.etree.ElementTree import parse
     
    daves_latitude = 41.98062
    daves_longitude = -87.668452
    
    doc = parse("rt22.xml")
     
    for bus in doc.findall('bus'):
        lat = float(bus.findall('lat')[0].text)
        if lat > daves_latitude:
            direction = bus.findall('d')[0].text
            if direction.startswith('North'):
                busid = bus.findall('id')[0]
                print (busid,lat)
    Last edited by bvdet; Apr 3 '15, 04:56 PM.

    Comment

    Working...