0
我有我的JSON文件,該文件是:錯誤而編輯JSON文件與Python
{
"DIR": "/home/yabir/code",
"DISK": [
"/home/yabir/back",
"455G",
"8,6G",
"423G",
"2%",
"/home/yabir/back"
],
"FIRST": false,
"last": "13012015_222441",
"last_save": [
"/home/yabir/code/pygame/puzzle.py",
"/home/yabir/code/pygame/memorypuzzle.py",
"/home/yabir/code/pygame/Project",
"A/elementos.xcf",
"/home/yabir/code/clank/src/database.py",
"/home/yabir/code/clank/src/engine.py",
"/home/yabir/code/clank/src/pruebas.py",
"/home/yabir/code/clank/src/.git/hooks/applypatch-msg.sample",
"/home/yabir/code/clank/src/.git/hooks/post-update.sample",
"/home/yabir/code/clank/src/.git/hooks/pre-applypatch.sample",
"/home/yabir/code/clank/src/.git/hooks/pre-rebase.sample",
"/home/yabir/code/clank/src/.git/hooks/commit-msg.sample",
"/home/yabir/code/clank/src/.git/hooks/update.sample",
"/home/yabir/code/clank/src/.git/hooks/pre-commit.sample",
"/home/yabir/code/clank/src/.git/hooks/pre-push.sample",
"/home/yabir/code/clank/src/.git/hooks/prepare-commit-msg.sample",
"/home/yabir/code/clank/src/.git/HEAD",
"/home/yabir/code/clank/src/.git/info/exclude",
"/home/yabir/code/clank/src/.git/config",
"/home/yabir/code/clank/src/.git/description",
"/home/yabir/code/clank/src/xml2json.py",
"/home/yabir/code/clank/src/LICENSE",
"/home/yabir/code/clank/src/main.py",
"/home/yabir/code/clank/src/save.json",
"/home/yabir/code/clank/src/main.pyc",
"/home/yabir/code/clank/src/disks_manager.py",
"/home/yabir/code/clank/src/xml2json.pyc",
"/home/yabir/code/clank/src/structure.py"
]
}
但是當我做
data["last_save"] = cmd_decoded
data["last"] = now
json_data.seek(0)
json_data.write(json.dumps(data,sort_keys=True,indent=4, separators=(',', ': ')))
其中cmd_decoded是字符串列表像JSON文件之前「 last_save」。它正確地替換了「最後」的值,但在此刻修改「last_save」,而不是python只是在最後的}
之後添加在json文件的末尾。最終結局:
{
"DIR": "/home/yabir/code",
"DISK": [
"/home/yabir/back",
"455G",
"8,6G",
"423G",
"2%",
"/home/yabir/back"
],
"FIRST": false,
"last": "13012015_222512",
"last_save": [
"/home/yabir/code/pygame/puzzle.py",
"/home/yabir/code/pygame/memorypuzzle.py",
"/home/yabir/code/pygame/Project",
"A/elementos.xcf",
"/home/yabir/code/clank/src/database.py",
"/home/yabir/code/clank/src/engine.py",
"/home/yabir/code/clank/src/pruebas.py",
"/home/yabir/code/clank/src/.git/hooks/applypatch-msg.sample",
"/home/yabir/code/clank/src/.git/hooks/post-update.sample",
"/home/yabir/code/clank/src/.git/hooks/pre-applypatch.sample",
"/home/yabir/code/clank/src/.git/hooks/pre-rebase.sample",
"/home/yabir/code/clank/src/.git/hooks/commit-msg.sample",
"/home/yabir/code/clank/src/.git/hooks/update.sample",
"/home/yabir/code/clank/src/.git/hooks/pre-commit.sample",
"/home/yabir/code/clank/src/.git/hooks/pre-push.sample",
"/home/yabir/code/clank/src/.git/hooks/prepare-commit-msg.sample",
"/home/yabir/code/clank/src/.git/HEAD",
"/home/yabir/code/clank/src/.git/info/exclude",
"/home/yabir/code/clank/src/.git/config",
"/home/yabir/code/clank/src/.git/description",
"/home/yabir/code/clank/src/xml2json.py",
"/home/yabir/code/clank/src/save.json",
"/home/yabir/code/clank/src/disks_manager.py",
"/home/yabir/code/clank/src/xml2json.pyc"
]
}/code/clank/src/main.pyc",
"/home/yabir/code/clank/src/disks_manager.py",
"/home/yabir/code/clank/src/xml2json.pyc",
"/home/yabir/code/clank/src/structure.py"
]
}
這應該是在「last_save」和「last_save」仍然像運行腳本之前一樣。爲什麼json文件沒有正確更改?
在我的文件begginig我:
json_data = open('save.json', "r+")
data = json.load(json_data)
,並在結尾:
json_data.close()
顯示打開文件的代碼,也是保存內容的一個例子,不清楚你的意思 – Anentropic
@Antropic編輯過,只是告訴我你是否需要更多 –