python - Get Data from Dict inside list inside dict -
i have output value elasticsearch in form
u'hits': { u'hits': [ { u'_score': 1.0, u'_type': u'timer_data', u'_id': u'au_uj1dk4uyhlwrlflqv', u'_source': { u'std': u'0', u'upper': u'62.688', u'lower': u'62.688', u'count_90': u'1', u'tgt': u'duration', u'grp': u'request', u'sum_90': u'62.688', u'sum': u'62.688', u'median': u'62.688', u'count': u'1', u'mean_90': u'62.688', u'sum_squares': u'3929.7853440000004', u'ns': u'gunicorn', u'act': u'', u'upper_90': u'62.688', u'sum_squares_90': u'3929.7853440000004', u'count_ps': u'0.1', u'@timestamp': u'1442809600000', u'mean': u'62.688' }, u'_index': u'statsd-2015.09.21' } ], u'total': 1, u'max_score': 1.0 }, u'_shards': { u'successful': 5, u'failed': 0, u'total': 5 }, u'took': 2, u'timed_out': false }
i want access data inside u'_source' dictionary cannot find proper way other following.
anoval = output[u'hits'][u'hits'] print type(anoval) # print output[u'hits'][u'hits'] anoval = anoval[0] print type(anoval)
i want know if there other more efficient way elasticsearch outputs coming dictionaries in python
try following recursively iterate through data structure , return searched value.
def search_multids(search_ds, searchfor): if isinstance(search_ds, dict): if searchfor in search_ds: return search_ds['_source'] else: key, value in search_ds.items(): return search_multids(value, searchfor) if isinstance(search_ds, list): item in search_ds: return search_multids(item, searchfor) else: return none print search_multids(s, '_source')
Comments
Post a Comment