我试图更新现有的Json文件,但由于某些原因,要求的值没有被改变,但整个值集(包括新的值)被追加到原始文件中。
jsonFile = open("replayScript.json", "r+")
data = json.load(jsonFile)
tmp = data["location"]
data["location"] = "NewPath"
jsonFile.write(json.dumps(data))
而结果是 。
"location": "NewPath",
"Id": "0",
"resultDir": "",
"resultFile": "",
"mode": "replay",
"className": "",
"method": "METHOD"
"location": "/home/karim/storm/project/storm/devqa/default.xml",
"Id": "0",
"resultDir": "",
"resultFile": "",
"mode": "replay",
"className": "",
"method": "METHOD"
"resultDir": "",
"location": "pathaaaaaaaaaaaaaaaaaaaaaaaaa",
"method": "METHOD",
"className": "",
"mode": "replay",
"Id": "0",
"resultFile": ""