6

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`.
4

1 回答 1

4

elasticsearch.py​​ 将json响应转换为python字典,方便提取信息。

IE

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

是python字典。

如果你想把它放在 json 结构中,那么你可以这样做,

json.dumps()

多看蟒蛇

于 2014-07-28T08:06:19.163 回答