如何将嵌套字典的格式更改为字典列表?

How to change format of a nested dictionary into a list of dictionaries?

我有一个嵌套字典,d1:

{'2020-12-10': {'EUR': 1.1379,
  'JPY': 128.75,
  'BGN': 1.9558,
  'CZK': 25.845,
  'DKK': 7.4641,
  'GBP': 0.90228},
 '2020-12-09': {'EUR': 1.1354,
  'JPY': 128.31,
  'BGN': 1.9558,
  'CZK': 25.886,
  'DKK': 7.463,
  'GBP': 0.88885},
 '2020-11-08': {'EUR': 1.1409,
  'JPY': 129.04,
  'BGN': 1.9558,
  'CZK': 26.002,
  'DKK': 7.4617,
  'GBP': 0.89108}}

我想把它改成这样的格式:

d2 = [{'date': '2020-12-10', 'target_currency': 'EUR', 'exchange_rate' : 1.1379},
        {'date': '2020-12-09', 'target_currency': 'EUR', 'exchange_rate' : 1.1354},
         ...
        ]

(稍后我需要使用 json.dumps() 在行分隔的 json 文件中进行转换)。

我已经尝试将此作为第一步,它将日期作为键。

d2 = {}
for k, v in d1.items():                     
    key = k
    d2[key] = {"date": k}   
  
    d2[key].update({k_:v_ for k_, v_ in v.items()})
    
d2   

{'2020-12-10': {'date': '2020-12-10',
  'EUR': 1.1379,
  'JPY': 128.75,
  'BGN': 1.9558,
  'CZK': 25.845,
  'DKK': 7.4641,
  'GBP': 0.90228},

需要一些指示如何从这里开始。谢谢。

尝试:

dct = {
    "2020-12-10": {
        "EUR": 1.1379,
        "JPY": 128.75,
        "BGN": 1.9558,
        "CZK": 25.845,
        "DKK": 7.4641,
        "GBP": 0.90228,
    },
    "2020-12-09": {
        "EUR": 1.1354,
        "JPY": 128.31,
        "BGN": 1.9558,
        "CZK": 25.886,
        "DKK": 7.463,
        "GBP": 0.88885,
    },
    "2020-11-08": {
        "EUR": 1.1409,
        "JPY": 129.04,
        "BGN": 1.9558,
        "CZK": 26.002,
        "DKK": 7.4617,
        "GBP": 0.89108,
    },
}

d2 = [
    {"date": k1, "target_currency": k2, "exchange_rate": v2}
    for k1, v1 in dct.items()
    for k2, v2 in v1.items()
]

print(d2)

打印:

[
    {"date": "2020-12-10", "target_currency": "EUR", "exchange_rate": 1.1379},
    {"date": "2020-12-10", "target_currency": "JPY", "exchange_rate": 128.75},
    {"date": "2020-12-10", "target_currency": "BGN", "exchange_rate": 1.9558},
    {"date": "2020-12-10", "target_currency": "CZK", "exchange_rate": 25.845},
    {"date": "2020-12-10", "target_currency": "DKK", "exchange_rate": 7.4641},
    {"date": "2020-12-10", "target_currency": "GBP", "exchange_rate": 0.90228},
    {"date": "2020-12-09", "target_currency": "EUR", "exchange_rate": 1.1354},
    {"date": "2020-12-09", "target_currency": "JPY", "exchange_rate": 128.31},
    {"date": "2020-12-09", "target_currency": "BGN", "exchange_rate": 1.9558},
    {"date": "2020-12-09", "target_currency": "CZK", "exchange_rate": 25.886},
    {"date": "2020-12-09", "target_currency": "DKK", "exchange_rate": 7.463},
    {"date": "2020-12-09", "target_currency": "GBP", "exchange_rate": 0.88885},
    {"date": "2020-11-08", "target_currency": "EUR", "exchange_rate": 1.1409},
    {"date": "2020-11-08", "target_currency": "JPY", "exchange_rate": 129.04},
    {"date": "2020-11-08", "target_currency": "BGN", "exchange_rate": 1.9558},
    {"date": "2020-11-08", "target_currency": "CZK", "exchange_rate": 26.002},
    {"date": "2020-11-08", "target_currency": "DKK", "exchange_rate": 7.4617},
    {"date": "2020-11-08", "target_currency": "GBP", "exchange_rate": 0.89108},
]