0
[u'10.57518117688789', u'43.17174576695126', u'0 10.57512669810526', u'43.17172389657181', u'0 10.57509460784044', u'43.17169116727101', u'0']

I need to turn this into a list of latitudes and longitudes that are in order from 1st to last. The first element being latitude and the second being longitude. I don't need the u or the '0'.

Right now, I'm just printing them, but this method needs to return a list of coordinates in order.

def get_coord_list_from_earth(filename):

    filename = str(filename)

    data = xml.dom.minidom.parse(filename)

    coordinates = data.getElementsByTagName('coordinates')[0].firstChild.nodeValue
    coordinates = coordinates.strip()

    print coordinates.split(',')

I need it to output a list of lists like this.

[10.57518117688789, 43.17174576695126], [10.57512669810526, 43.17172389657181], [10.57509460784044, 43.17169116727101]

This link is to a sample file this would need to run with

https://www.dropbox.com/s/8heebhnmlwvjtl7/earthFile.xml

Will Luce
  • 1,781
  • 3
  • 20
  • 33

2 Answers2

2

Can be done easily with zip:

[[float(item[0]), float(item[1])] for item in zip(coordinates[0::2], coordinates[1::2])]

See usage here: http://ideone.com/j4O48c

Sufian Latif
  • 13,086
  • 3
  • 33
  • 70
1

Could you please check this:

Let me know if that works for you:

import xml.dom.minidom

def get_coord_list_from_earth(filename):

    filename = str(filename)

    data = xml.dom.minidom.parse(filename)

    coordinates = data.getElementsByTagName('coordinates')[0].firstChild.nodeValue
    coordinates = str(coordinates.strip())

    lol = list()
    ls = coordinates.split(',')
    for group_ls in zip(ls[0::2], ls[1::2]):
        f = group_ls[0].split()[-1]
        s = group_ls[1].split()[-1]
        # creating list of tuples, if you are not going to mutate it 
        # then tuple is must more memory efficient then list.
        lol.append((f,s))
    return lol

print get_coord_list_from_earth('test.xml')

Output:

[('-99.96592053692414', '35.92662037784583'), ('-99.96540056429473', 
  '35.92663981781373'), ('-99.96498447443297', '35.92665534578857'), 
 ('-99.96454642236132', '35.9264185376019')]
James Sapam
  • 16,036
  • 12
  • 50
  • 73