2015-01-13 75 views
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() 
+0

顯示打開文件的代碼,也是保存內容的一個例子,不清楚你的意思 – Anentropic

+0

@Antropic編輯過,只是告訴我你是否需要更多 –

回答

2

你的腳本打開一個文件,從中讀取,然後試圖回到它的起點,並寫入東西到它,取代它的部分內容。但是,如果您正在編寫的內容小於文件的原始內容,則這隻會覆蓋您正在編寫的相同數量的數據。

看起來你想在寫入文件時完全替換文件的內容。您應該在"r"中打開它以進行初始解析,然後然後關閉它並在"w"中重新打開它以向其寫入數據。