【发布时间】:2018-08-29 05:25:23
【问题描述】:
我正在尝试弄清楚如何将添加在一起的键保存在一起。我编写了这个小脚本,每次运行时都会添加键和值,但它会不断添加字典。
import json
import datetime
key = str(datetime.datetime.now())
insert = 'Test'
logged = {key : insert}
data = json.load(open('StartLog.json', 'r'))
with open('StartLog.json', 'w') as f:
data['Logs'].append(logged)
json.dump(data, f, indent=2)
当前的输出是:
{
"Logs": [
{
"2018-08-28 22:07:12.540188": "Test"
},
{
"2018-08-28 22:07:20.134817": "Test"
}
]
}
我希望输出是:
{
"Logs": [
{
"2018-08-28 22:07:12.540188": "Test",
"2018-08-28 22:07:20.134817": "Test"
}
]
}
【问题讨论】:
标签: python json python-3.x