格式化elasticsearch-py的输出 [英] Format the output of elasticsearch-py
本文介绍了格式化elasticsearch-py的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将 python
客户端用于 elasticsearch
。这是一个最小的示例:
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')
输出为:
{u'count': 836780, u'_shards': {u'successful': 5, u'failed': 0, u'total': 5}}
我有两个问题:
- 如何摆脱
u'
(u
后跟单引号)? -
如何提取count的值?我想我可以进行字符串操作,但这听起来像是错误的方式...。
答案:如果输出保存到res
,然后res ['count']返回数字
836780`。
- How do I get rid of the
u'
(u
followed by a single quote )? 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 tores
, thenres['count'] returns the number
836780`.
推荐答案
elasticsearch.py将json响应转换为python字典,以便轻松提取信息。
elasticsearch.py convert json response to dictionary for python, so that it is easy to extract information.
即
{u'count': 836780, u'_shards': {u'successful': 5, u'failed': 0, u'total': 5}}
是python字典。
is python dictionary.
如果您希望将其包含在json结构中,则可以这样做,
If you wan to have it in json structure then you can do,
json.dumps()
查找更多 python
这篇关于格式化elasticsearch-py的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文