如何读取 JSON 文件,修改并写入?

huangapple go评论63阅读模式
英文:

python3 How to read json file, modify and write?

问题

以下是代码的翻译部分:

with open(os.environ.get("WORKSPACE") + "/test.json", 'r+') as test_file:
    test = json.load(test_file)
    test['COMPONENTS'] = "test components"
    json.dump(test, test_file)

请注意,您的代码的问题在于以'r+'模式打开文件,这导致了不正确的文件操作。您应该使用'w'模式,以便在写入新数据之前清空文件内容。这里是修正后的代码:

with open(os.environ.get("WORKSPACE") + "/test.json", 'w') as test_file:
    test = json.load(test_file)
    test['COMPONENTS'] = "test components"
    json.dump(test, test_file)

另外,根据您提供的预期结果,您可能还需要将test对象写回文件,以确保文件中只包含修改后的 JSON 数据。可以在上面的代码块之后添加以下行:

test_file.seek(0)  # 将文件指针移动到文件开头
json.dump(test, test_file)  # 写入修改后的 JSON 数据
test_file.truncate()  # 截断文件,确保不会保留多余的数据

这将确保文件中仅包含修改后的 JSON 数据。

英文:

I am trying to read a json file, modify and then save the modified version.
Unfortunately, the content of the file, instead of being saved, adds another json to the end of the original one.

my code:

with open(os.environ.get("WORKSPACE")+"/test.json", 'r+') as test_file:
    test = json.load(test_file)
    test['COMPONENTS'] = "test components"
    json.dump(test, test_file)

test.json
{"STAGE": "Test", "DATE": "2023-02-17", "TIME": "13:27", "COMPONENTS": ""}

after running code
{"STAGE": "Test", "DATE": "2023-02-17", "TIME": "13:27", "COMPONENTS": ""}{"STAGE": "Test", "DATE": "2023-02-17", "TIME": "13:27", "COMPONENTS": "test components"}

expected result:
{"STAGE": "Test", "DATE": "2023-02-17", "TIME": "13:27", "COMPONENTS": "test components"}

Please point out what I am doing wrong

My environment
Python 3.10.9
macos 12.3.1

I try to use w+

Exception has occurred: JSONDecodeError
Expecting value: line 1 column 1 (char 0)
StopIteration: 0

During handling of the above exception, another exception occurred:

  File "/test.py", line 20, in <module>
    test = json.load(test_file)
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

答案1

得分: 1

我认为问题在于在你的打开模式中添加了一个 +。另外请注意,w+ 会截断文件,所以不会有 JSON 可以读取。我认为你应该这样做:

with open(os.environ.get("WORKSPACE")+"/test.json", 'r') as test_file:
    test = json.load(test_file)
    test['COMPONENTS'] = "test components"

with open(os.environ.get("WORKSPACE")+"/test.json", 'w') as test_file:
    json.dump(test, test_file)
英文:

I think the problem is adding a + to your opening modes. Also note that w+ would truncate the file, so there will be no JSON to read. I think what you should do is:

with open(os.environ.get("WORKSPACE")+"/test.json", 'r') as test_file:
    test = json.load(test_file)
    test['COMPONENTS'] = "test components"

with open(os.environ.get("WORKSPACE")+"/test.json", 'w') as test_file:
    json.dump(test, test_file)

答案2

得分: 1

以下是您要翻译的内容:

The easiest way to do it, like UpmostScarab said, is to just open the file twice. If you only want to open it once for some reason, you can read, then seek(0), then write, and finally truncate:

with open(os.environ.get("WORKSPACE") + "/test.json", 'r+') as test_file:
    test = json.load(test_file)
    test['COMPONENTS'] = "test components"
    test_file.seek(0)
    json.dump(test, test_file)
    test_file.truncate()
英文:

The easiest way to do it, like UpmostScarab said, is to just open the file twice. If you only want to open it once for some reason, you can read, then seek(0), then write, and finally truncate:

with open(os.environ.get("WORKSPACE")+"/test.json", 'r+') as test_file:
    test = json.load(test_file)
    test['COMPONENTS'] = "test components"
    test_file.seek(0)
    json.dump(test, test_file)
    test_file.truncate()

huangapple
  • 本文由 发表于 2023年2月17日 23:33:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75486280.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定