Python 2.7:为什么json.loads无法将我的字符串正确转换为dict?

时间:2018-07-30 12:22:22

标签: python json elasticsearch

我要一个ElasticSearch数据库使用Python 2.7和Requests包向我提供索引列表及其创建日期。想法是快速计算哪些索引超出了保留策略,需要进入休眠状态。

请求工作正常,结果正是我想要的。但是,当我运行下面的代码时,当我尝试将json结果转换为dict时,DiDict的类型是正确的,但是它报告的大小为1,此时应该至少有几十个条目。我究竟做错了什么?我觉得这真的很愚蠢,但我实在无法接受! :)

import json
import requests

esEndPoint = "https://localhost:9200"
retrieveString = "/_cat/indices?h=index,creation.date.string&format=json&s=creation.date"

# Gets the current indices and their creation dates

def retrieveIndicesAndDates():
    try:
        theResult = requests.get(esEndPoint+retrieveString)
        print (theResult.content)
    except Exception as e:
        print("Unable to retrieve list of indices with creation dates.")
        print("Error: "+e)
        exit(3)
    return theResult.content

def main():
    theDict = dict(json.loads(retrieveIndicesAndDates()))
    print(type(theDict)) # Reports correct type
    print(len(theDict))  # Always outputs "1" ??
    for index, creationdate in theDict.items():
        print("Index: ",index,", Creation date: ",theDict[index])
    return

调用返回的json:

[{"index":".kibana","creation.date.string":"2017-09-14T15:01:38.611Z"},{"index":"logstash-2018.07.23","creation.date.string":"2018-07-23T00:00:01.024Z"},{"index":"cwl-2018.07.23","creation.date.string":"2018-07-23T00:00:03.877Z"},{"index":"k8s-testing-internet-2018.07.23","creation.date.string":"2018-07-23T14:19:10.024Z"},{"index":"logstash-2018.07.24","creation.date.string":"2018-07-24T00:00:01.023Z"},{"index":"k8s-testing-internet-2018.07.24","creation.date.string":"2018-07-24T00:00:01.275Z"},{"index":"cwl-2018.07.24","creation.date.string":"2018-07-24T00:00:02.157Z"},{"index":"k8s-testing-internet-2018.07.25","creation.date.string":"2018-07-25T00:00:01.022Z"},{"index":"logstash-2018.07.25","creation.date.string":"2018-07-25T00:00:01.186Z"},{"index":"cwl-2018.07.25","creation.date.string":"2018-07-25T00:00:04.012Z"},{"index":"logstash-2018.07.26","creation.date.string":"2018-07-26T00:00:01.026Z"},{"index":"k8s-testing-internet-2018.07.26","creation.date.string":"2018-07-26T00:00:01.185Z"},{"index":"cwl-2018.07.26","creation.date.string":"2018-07-26T00:00:02.587Z"},{"index":"k8s-testing-internet-2018.07.27","creation.date.string":"2018-07-27T00:00:01.027Z"},{"index":"logstash-2018.07.27","creation.date.string":"2018-07-27T00:00:01.144Z"},{"index":"cwl-2018.07.27","creation.date.string":"2018-07-27T00:00:04.485Z"},{"index":"ctl-2018.07.27","creation.date.string":"2018-07-27T09:02:09.854Z"},{"index":"cfl-2018.07.27","creation.date.string":"2018-07-27T11:12:44.681Z"},{"index":"elb-2018.07.27","creation.date.string":"2018-07-27T11:13:51.340Z"},{"index":"cfl-2018.07.24","creation.date.string":"2018-07-27T11:45:23.697Z"},{"index":"cfl-2018.07.23","creation.date.string":"2018-07-27T11:45:24.646Z"},{"index":"cfl-2018.07.25","creation.date.string":"2018-07-27T11:45:25.700Z"},{"index":"cfl-2018.07.26","creation.date.string":"2018-07-27T11:45:26.341Z"},{"index":"elb-2018.07.24","creation.date.string":"2018-07-27T11:45:27.440Z"},{"index":"elb-2018.07.25","creation.date.string":"2018-07-27T11:45:29.572Z"},{"index":"elb-2018.07.26","creation.date.string":"2018-07-27T11:45:36.170Z"},{"index":"logstash-2018.07.28","creation.date.string":"2018-07-28T00:00:01.023Z"},{"index":"k8s-testing-internet-2018.07.28","creation.date.string":"2018-07-28T00:00:01.316Z"},{"index":"cwl-2018.07.28","creation.date.string":"2018-07-28T00:00:03.945Z"},{"index":"elb-2018.07.28","creation.date.string":"2018-07-28T00:00:53.992Z"},{"index":"ctl-2018.07.28","creation.date.string":"2018-07-28T00:07:19.543Z"},{"index":"k8s-testing-internet-2018.07.29","creation.date.string":"2018-07-29T00:00:01.026Z"},{"index":"logstash-2018.07.29","creation.date.string":"2018-07-29T00:00:01.378Z"},{"index":"cwl-2018.07.29","creation.date.string":"2018-07-29T00:00:04.100Z"},{"index":"elb-2018.07.29","creation.date.string":"2018-07-29T00:00:59.241Z"},{"index":"ctl-2018.07.29","creation.date.string":"2018-07-29T00:06:44.199Z"},{"index":"logstash-2018.07.30","creation.date.string":"2018-07-30T00:00:01.024Z"},{"index":"k8s-testing-internet-2018.07.30","creation.date.string":"2018-07-30T00:00:01.179Z"},{"index":"cwl-2018.07.30","creation.date.string":"2018-07-30T00:00:04.417Z"},{"index":"elb-2018.07.30","creation.date.string":"2018-07-30T00:01:01.442Z"},{"index":"ctl-2018.07.30","creation.date.string":"2018-07-30T00:08:28.936Z"},{"index":"cfl-2018.07.30","creation.date.string":"2018-07-30T06:52:16.739Z"}]

1 个答案:

答案 0 :(得分:2)

您的错误正在尝试将字典列表转换为字典:

theDict = dict(json.loads(retrieveIndicesAndDates()))
#         ^^^^^                                     ^

这仅适用于列表的 dict 。但这是多余的。

直接使用回复即可。每个条目都是具有相应键的字典:

data = json.loads(retrieveIndicesAndDates())
for entry in data:
    print("Index: ", entry["index"], ", Creation date: ", entry["creation.date.string"])

那么当您将list转换为dict时会发生什么呢?为什么只有一个条目?

dict理解三种初始化方法:关键字,映射和可迭代。 list适合最后一个。

从可迭代对象进行初始化会经历它,并期望键值可迭代对象作为元素。如果要手动进行操作,则如下所示:

def sequence2dict(sequence):
    map = {}
    for element in sequence:
        key, value = element
        map[key] = value
    return map

注意每个元素如何通过迭代解压缩吗?在回复中,每个元素都是一个{<1>},其中有两个条目。对此进行迭代会产生两个,但会忽略这些值。

dict

对于key, value = {"index":".kibana","creation.date.string":"2017-09-14T15:01:38.611Z"} print(key, '=>', value) # prints "index => creation.date.string" 构造函数,答复中的每个元素都具有相同的键值对:dict"index"。由于字典中的键是唯一的,因此所有元素都折叠到同一条目:"creation.date.string"