【发布时间】:2023-03-19 05:03:01
【问题描述】:
ohlc={58072071: {datetime.datetime(2021, 3, 26, 23, 20):
{'high': 179.0, 'low': 179.0, 'open': 179.0, 'close': 179.0,
'volume': 2354}}}
如何将此字典写入 JSON 文件?
当我将其转储到 JSON 文件时出现此错误
TypeError(f'keys must be str, int, float, bool or None, '
TypeError: keys must be str, int, float, bool or None, not datetime
【问题讨论】:
-
这个错误很明显,因为
json数据中有datetime对象,而datetime对象不是json serializable。 -
这能回答你的问题吗? How to write a nested dictionary to json
-
这个答案指定了如何 JSON 序列化任何东西,包括具体的日期时间:stackoverflow.com/questions/11875770/…
-
能否在您的问题中添加触发此错误的代码?
标签: python json dictionary nested