xml - Split an Ordered Dictionary into variables in Python -


here snippet of code far

import osmapi import geopy geopy.geocoders import nominatim import requests import xmltodict  geolocator = nominatim() location = geolocator.reverse("{}, {}".format(lat, lon)) dict = location.raw osmid = dict.get('osm_id', 'default_value_if_null_here') osmtype = dict.get('osm_type', 'default_value_if_null_here')  if(osmtype == 'node'):     node_info = requests.get("http://api.openstreetmap.org/api/0.6/node/"+ osmid)     d = xmltodict.parse(node_info.content)     amenity_tags = [tag tag in d['osm']['node']['tag'] if tag['@k'] == 'amenity']      if len(amenity_tags) != 0:         print amenity_tags 

i want check if location corresponding node on openstreetmap , if so, check if amenity , type of amenity. sample output follows:

[ordereddict([(u'@k', u'amenity'), (u'@v', u'cafe')])] [ordereddict([(u'@k', u'amenity'), (u'@v', u'fast_food')])] [ordereddict([(u'@k', u'amenity'), (u'@v', u'bicycle_parking')])] [ordereddict([(u'@k', u'amenity'), (u'@v', u'atm')])] [ordereddict([(u'@k', u'amenity'), (u'@v', u'restaurant')])] [ordereddict([(u'@k', u'amenity'), (u'@v', u'restaurant')])] [ordereddict([(u'@k', u'amenity'), (u'@v', u'theatre')])] 

my question how split ordered dictionary. outcome hoping have variable 'amenity_type' = cafe, restaurant, theatre etc... hope have explained question enough. new python appreciated, thanks!

perhaps you're looking class. class gives ability hold complex data in 1 bucket, were, can provide custom string services formatting print, allows define operators objects of type. example takes (up to) 3 variables in initialization or via set(), , output them when use str()

#!/usr/bin/python  class mything(object):     def __init__(self,a='',b=0,c=[]):         self.set(a,b,c);     def set(self,a=none,b=none,c=none):         if != none: self.a = a;         if b != none: self.b = b;         if c != none: self.c = c;     def __str__(self):         out  = self.a + '\n'         out += str(self.b) + '\n'         out += str(self.c) + '\n'         return out  if __name__ == "__main__":     x = mything('boooga',42,[1,2,'spaghetti , pizza'])     print x      x.set(b=12)     print str(x)      print x.a     print str(x.b)     print str(x.c)     el in x.c:         print el 

if put in file, , run @ console, get:

boooga 42 [1, 2, 'spaghetti , pizza']  boooga 12 [1, 2, 'spaghetti , pizza']  boooga 12 [1, 2, 'spaghetti , pizza'] 1 2 spaghetti , pizza 

there's lots more can class in vein, perhaps started if suits needs.

i'm assuming running linux or os x. if won't run in either of those, type which python @ console, , change first line /usr/bin/python instance reports you.

it's written python 2-series. python 3 is... else.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -