如何在python3中对嵌套字典进行反向排序?

时间:2019-02-11 15:41:02

标签: json python-3.x dictionary reverse simplejson

很抱歉,尽管我在全面搜索后仍无法找到答案,但已经问过类似的问题。我一直在努力将以下字典导出到JSON文件中,并按降序对键进行排序。该文件需要在Python外可读,因此不能使用pickle。

我正在使用Python3.7,并且尝试使用simplejson和json库将嵌套的dict转储到json文件中,尽管失败了。 Pickle似乎可以工作,尽管它导出的二进制文件在Python之外是不可读的。词典称为“现有”。

with open('Entries_2.json', 'w') as outfile:
    simplejson.dump(existing,outfile,item_sort_key=simplejson.simple_first)
outfile.close()

with open('Entries.json', 'w') as outfile:
    json.dump(existing, outfile, sort_keys=True, indent=4)
outfile.close()

以上两种方法都会产生相同的结果:

{
    "Entries": {
        "2019/01/23": {
            "Result-9595905890": {
                "count": 4,
                "time": "2019/01/23 03:32:32"
            }
        },
        "2019/01/24": {
            "Result-9607169713": {
                "count": 21,
                "time": "2019/01/24 03:31:34"
            },
            "Result-9611777668": {
                "count": 23,
                "time": "2019/01/24 12:58:49"
            }
        },
        "2019/01/25": {
            "Result-9618433556": {
                "count": 21,
                "time": "2019/01/25 03:31:27"
            }
        }
    }
}

尽管reverse = True选项在两个转储选项中均不起作用。 我需要的是:

{
    "Entries": {
        "2019/01/25": {
            "Result-9618433556": {
                "count": 21,
                "time": "2019/01/25 03:31:27"
            }
        },
        "2019/01/24": {
            "Result-9607169713": {
                "count": 21,
                "time": "2019/01/24 03:31:34"
            },
            "Result-9611777668": {
                "count": 23,
                "time": "2019/01/24 12:58:49"
            }
        },
        "2019/01/23": {
            "Result-9595905890": {
                "count": 4,
                "time": "2019/01/23 03:32:32"
            }
        }
    }
}

有人遇到过类似的斗争吗?

1 个答案:

答案 0 :(得分:0)

尝试查看collections.OrderedDict

import json, collections

existing = {
    "Entries": {
        "2019/01/23": {
            "Result-9595905890": {
                "count": 4,
                "time": "2019/01/23 03:32:32"
            }
        },
        "2019/01/24": {
            "Result-9607169713": {
                "count": 21,
                "time": "2019/01/24 03:31:34"
            },
            "Result-9611777668": {
                "count": 23,
                "time": "2019/01/24 12:58:49"
            }
        },
        "2019/01/25": {
            "Result-9618433556": {
                "count": 21,
                "time": "2019/01/25 03:31:27"
            }
        }
    }
}

new_entries = collections.OrderedDict(reversed(sorted(existing['Entries'].items()))) # if you want reversed sorted
existing['Entries'] = new_entries

with open('Entries.json', 'w') as outfile:
    json.dump(existing, outfile, indent=4)

顺便说一句,当您执行with open('Entries.json', 'w') as outfile:时,它将在with语句完成后自动关闭outfile,因此您无需显式关闭它。

输出:

{
    "Entries": {
        "2019/01/25": {
            "Result-9618433556": {
                "count": 21,
                "time": "2019/01/25 03:31:27"
            }
        },
        "2019/01/24": {
            "Result-9607169713": {
                "count": 21,
                "time": "2019/01/24 03:31:34"
            },
            "Result-9611777668": {
                "count": 23,
                "time": "2019/01/24 12:58:49"
            }
        },
        "2019/01/23": {
            "Result-9595905890": {
                "count": 4,
                "time": "2019/01/23 03:32:32"
            }
        }
    }
}
相关问题