简体   繁体   中英

Get Data from Dict inside list inside dict

I have a output value from ElasticSearch which is in this 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 to access data inside u'_source' dictionary but cannot find any proper way other than following.

anoval = output[u'hits'][u'hits']
print type(anoval)
# print output[u'hits'][u'hits']
anoval = anoval[0]
print type(anoval)

I want to know if there is any other more efficient way to do this with elasticsearch outputs coming as dictionaries in python

Try the following to recursively iterate through you data structure and return the searched value.

def search_multiDS(search_DS, searchfor):
    if isinstance(search_DS, dict):
        if searchfor in search_DS:
            return search_DS['_source']
        else:
            for key, value in search_DS.items():
                return search_multiDS(value, searchfor)
    if isinstance(search_DS, list):
        for item in search_DS:
            return search_multiDS(item, searchfor)
    else:
        return None

print search_multiDS(s, '_source')

Your '_source' dict is inside a list. So you have to access that lists first item to access the '_source' .
so try:

['hits']['hits'][0]['_source']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM