tabchas
tabchas

Reputation: 1402

Python Strings and Dictionaries Parsing

I have this output from using the readLocation() method from the SL4A API. This is the result. How can I parse this so I only get altitude, longitude, and latitude?

    Result(id=2, result={u'network': {u'altitude': 0, u'p
    rovider': u'network', u'longitude': -97.6591116000000
    03, u'time': 1329794430482L, u'latitude': 30.44257223
    3333333, u'speed': 0, u'accuracy': 51}}, error=None)

I tried it with this code:

   import android, string, time

   droid = android.Android()
   droid.startLocating()
   time.sleep(5)

   res = droid.readLocation()

   print res.result[u'latitude']

It returned: KeyError: u'latitude'

Upvotes: 0

Views: 237

Answers (3)

avasal
avasal

Reputation: 14854

try

res.result[u'network'][u'latitude']

since network acts as the key to dictionary containing altitude, longitude & latitude

u'network' : { u'altitude': 0, u'provider': u'network',u'longitude': -97.6591116000000    03, u'time': 1329794430482L, u'latitude': 30.442572233333333, u'speed': 0, u'accuracy': 51}

Upvotes: 1

Andrey Sobolev
Andrey Sobolev

Reputation: 12693

As res.result obviously consists of nested dicts, res.result[u'network'][u'latitude'] should do the trick.

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798676

Assuming res is the name bound to that object, res.result[u'network'][u'altitude'] will get you the altitude. Modify as appropriate for the other keys.

Upvotes: 1

Related Questions