简体   繁体   English

格式化elasticsearch-py的输出

[英]Format the output of elasticsearch-py

I'm trying to use the python client for elasticsearch . 我正在尝试使用python客户端进行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 )? 我如何摆脱u'u跟着一个单引号)?
  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 number 836780`. 我想我可以做字符串操作,但这听起来像是错误的方式....答案:如果输出保存到res ,则res['count'] returns the number 836780`。

elasticsearch.py convert json response to dictionary for python, so that it is easy to extract information. elasticsearch.py​​将json响应转换为python的字典,以便于提取信息。

Ie

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

is python dictionary. 是python字典。

If you wan to have it in json structure then you can do, 如果你想在json结构中使用它,那么你可以这样做,

json.dumps()

look more python 看起来更蟒蛇

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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