使用python编辑json文件时出错

时间:2015-01-13 21:34:48

标签: python json

我的json文件是:

{
    "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是“last_save”之前的json文件中的字符串列表。它正确地替换了“last”值但是现在修改“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文件没有正确更改?

在我的文件的开头,我有:

json_data = open('save.json', "r+")
data = json.load(json_data)

最后:

json_data.close()

1 个答案:

答案 0 :(得分:2)

您的脚本会打开一个文件,从中读取文件,然后回读它的开头并将内容写入其中,并在其内容中替换部分内容。但是,如果您所写的内容小于文件的原始内容,则只会覆盖您正在编写的相同数量的数据。

在写入文件时,您似乎想要完全替换文件的内容。您应该在"r"中打开它进行初始解析,然后关闭它并在"w"中重新打开它以向其写入数据。