【发布时间】:2016-11-10 14:44:35
【问题描述】:
我想在我的json 文件中更新float values,结构如下:
{"Starbucks": {"Roads": 1.0, "Pyramid Song": 1.0, "Go It Alone": 1.0}}
因此,每当我使用完全相同的项目生成一个已经存在的播放列表时,我都会将 key values 增加 +1.0。
我有一个使用'append' 选项打开的文件
with open('pre_database/playlist.json', 'a') as f:
if os.path.exists('pre_database/playlist.json'):
#update json here
json.dump(playlist,f)
但是这个'a' 方法会将另一个dictionary 附加到json,并且稍后会产生parsing 问题。
同样,如果我使用'w' 方法,它会完全覆盖文件。
更新值的最佳解决方案是什么?
【问题讨论】: