如何在 python3 中反转嵌套字典的顺序?

How to reverse order nested dictionary in python3?

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

我正在使用 Python3.7,我尝试使用简单的json 和 json 库将嵌套的字典转储到 json 文件中,尽管有没有成功。 Pickle 似乎可以工作,尽管它会导出一个在 Python 之外无法读取的二进制文件。字典被称为 'existing'.

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"
            }
        }
    }
}

有没有人遇到过类似的问题?

尝试查看 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"
            }
        }
    }
}