Printing out the contents of an object to an HTML page using Django

Hi
I have a library which i am trying to use but I don't know how to access parts of the returned object

The code I run in python is:
from boris import BikeChecker
from pprint import PrettyPrinter
bc = BikeChecker()
pp = PrettyPrinter()
bike_predicate = lambda x: x['nbBikes'] >= 2
nearest = bc.find_with_geo(51.49, -0.19, predicate=bike_predicate)
pp.pprint(nearest)

Open in new window


which outputs the following:
C:\Python27\python.exe C:/Users/nicka_000/Documents/Coding/websites_go_here/testing/borisbikes.py
{'distance': 0.032766888603455446,
 'station': {'id': 219,
             'installDate': 1279545900000L,
             'installed': True,
             'lat': 51.49016361,
             'locked': False,
             'long': -0.19039362,
             'name': u"Bramham Gardens, Earl's Court",
             'nbBikes': 8,
             'nbDocks': 29,
             'nbEmptyDocks': 20,
             'removalDate': None,
             'temporary': False,
             'terminalName': u'001183'}}

Process finished with exit code 0
 what I  want to do is access the lat and long variables within that object and store for later use. How would I do that

thanks very much
lz7cjcAsked:
Who is Participating?
 
clockwatcherCommented:
You've got a dictionary of dictionaries.  Not an object with attributes.  You'd reference it like this:
nearest["station"]["lat"]

Open in new window

0
 
nap0leonCommented:
Reference: http://www.tutorialspoint.com/json/json_python_example.htm

setting the variable "nearest" to the JSON, you might access it as like
nearest.station.lat
nearest.station.long

Open in new window

0
 
lz7cjcAuthor Commented:
thanks for the article but I still don't get it - sorry!

I tried nearest.station.lat and I get the following error:
AttributeError: 'dict' object has no attribute 'station'


So I did this:

import demjson
lat = demjson.decode(nearest)
print lat

Open in new window


where am I going wrong?
thanks
0
 
lz7cjcAuthor Commented:
thank you
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.