【问题标题】:When I modify a value in a json of nested dicts it is removing other dicts当我修改嵌套字典的 json 中的值时,它正在删除其他字典
【发布时间】:2021-03-30 13:07:49
【问题描述】:

我想弄清楚如何修改 json 文件中的值。我已将其简化为两个字典的字典。我可以修改 json 文件中的值,但生成的 json 文件不再是字典的字典,只是包含我修改的值的一个字典。有什么想法可以解决这个问题吗?

原始 json 文件

{
    "test_settings":
        {
            "rigID":"r1",
            "test":"blank"
        },
    "temperature_settings":
        {
            "temperature_start":"40.1",
            "temperature_stop":"blank",
            "temperature_step":"blank"
        }
}

我的代码

import json

def modify_json_file(json_file):
    with open(json_file, "r") as input_json:
        json_data = json.load(input_json)
        print("type of data: ", type(json_data))
        for k,v in json_data.items():
            print(k,v)
        print('\n'*2)
        temperature_settings = json_data["temperature_settings"]
        print("type of temperature_settings: ", type(temperature_settings))
        print(temperature_settings["temperature_start"])
    temperature_settings["temperature_start"] = 99.9
    with open(json_file, "w") as input_json:
        json_data = json.dump(temperature_settings, input_json)


print('\n'*25)
modify_json_file("file6.json")

现在听写

{"temperature_start": 99.9, "temperature_stop": "blank", "temperature_step": "blank"}

想要的结果

{
    "test_settings":
        {
            "rigID":"r1",
            "test":"blank"
        },
    "temperature_settings":
        {
            "temperature_start":"99.9",
            "temperature_stop":"blank",
            "temperature_step":"blank"
        }
}

【问题讨论】:

    标签: python json dictionary


    【解决方案1】:

    好吧,你只回信temperature_settings,所以你只会得到那个......只需使用:

    with open(json_file, "w") as input_json:
        json.dump(json_data, input_json)
    

    得到预期的结果。

    【讨论】:

    • 嗨,Serge,它保留了其他字典,但正在更改 json 格式(我不知道这是否正常)。 {“test_settings”:{“rigID”:“r1”,“test”:“blank”},“temperature_settings”:{“temperature_start”:99.9,“temperature_stop”:“blank”,“temperature_step”:“blank”} }
    • 这保留了格式:json_data = json.dump(json_data, input_json, indent = 4)
    【解决方案2】:

    在写入前设置json_data的值。

        with open(json_file, "w") as input_json:
            json_data["temperature_settings"]=temperature_settings        
            json.dump(json_data, input_json)
    

    【讨论】:

      【解决方案3】:

      试试下面的

      
      
      def modify_json_file(json_file):
          with open(json_file, "r") as input_json:
              json_data = json.load(input_json)
              print("type of data: ", type(json_data))
              for k, v in json_data.items():
                  print(k, v)
              print('\n'*2)
              temperature_settings = json_data["temperature_settings"]
              print("type of temperature_settings: ", type(temperature_settings))
              print(temperature_settings["temperature_start"])
              test_settings = json_data["test_settings"]
          temperature_settings["temperature_start"] = 99.9
          new_settings = []
          new_settings.append(test_settings)
          new_settings.append(temperature_settings)
          with open(json_file, "w") as input_json:
              json_data = json.dump(new_settings, input_json)
      
      
      print('\n'*25)
      modify_json_file("file6.json")```
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-06-19
        • 2022-06-13
        • 2018-09-13
        • 1970-01-01
        • 2021-12-13
        • 2018-03-27
        • 2018-05-31
        • 2021-02-23
        相关资源
        最近更新 更多