Format the output of elasticsearch-py

微笑、不失礼 提交于 2019-12-04 03:30:27

问题


I'm trying to use the python client for elasticsearch. Here is a minimal example:

import logging
logging.basicConfig()

from elasticsearch import Elasticsearch as ES

print "Setup connection..."
es=ES(['localhost:8080'])
print "Done!"

print "Count number of users..."
print es.count(index='users')

The output is:

{u'count': 836780, u'_shards': {u'successful': 5, u'failed': 0, u'total': 5}}

I have two questions:

  1. How do I get rid of the u' (u followed by a single quote )?
  2. How can I extract the value of count? I guess I could do string manipulation, but that sounds like the wrong way.... Answer: if the output is saved to res, then res['count'] returns the number836780`.

回答1:


elasticsearch.py convert json response to dictionary for python, so that it is easy to extract information.

I.e

{u'count': 836780, u'_shards': {u'successful': 5, u'failed': 0, u'total': 5}}

is python dictionary.

If you wan to have it in json structure then you can do,

json.dumps()

look more python



来源:https://stackoverflow.com/questions/24990706/format-the-output-of-elasticsearch-py

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!