格式化elasticsearch-py的输出

时间:2014-07-28 07:51:22

标签: python elasticsearch pyelasticsearch

我正在尝试将python客户端用于elasticsearch。这是一个最小的例子:

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')

输出结果为:

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

我有两个问题:

  1. 如何摆脱u'u后跟单引号)?
  2. 如何提取计数值?我想我可以做字符串操作,但这听起来像是错误的方式...... 答:如果输出保存到res,则res['count'] returns the number 836780`。

1 个答案:

答案 0 :(得分:4)

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

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

是python字典。

如果你想在json结构中使用它,那么你可以做到,

json.dumps()

查看更多python