【问题标题】:python: read json data from file and append more datapython:从文件中读取json数据并附加更多数据
【发布时间】:2016-04-09 02:41:18
【问题描述】:

我正在尝试从 json 文件中读取现有数据,并尝试使用 python 将更多数据附加到文件中(我是 python 新手)。这是我在脚本中读取的 data.json 文件中的现有数据:

{
    "Config1": {
        "TestCase1": {
            "Data1": 200,
            "Data2": 2715
        }
    },
    "Config2": {
        "TestCase1": {
            "Data1": 2710,
            "Data2": 2715
        }
    }
}

阅读后我想附加 TestCase2 数据。这就是我正在做的:

with open("data.json") as json_file: #load existing data
    json_data = json.load(json_file)

test='TestCase2'
result=json_data
myConfigs = ['Config1','Config2']
for each, config in enumerate(myConfigs):
    result.update({config:{test:{'Data1':2600,'Data2':2900}}})
with open('data.json', 'a') as outfile:
    json.dump(result, outfile)

jsonLint 指出,data.json 中的新数据无效。我究竟做错了什么?这是新数据

{
        "Config1": {
            "TestCase1": {
                "Data1": 200,
                "Data2": 2715
            }
        },
        "Config2": {
            "TestCase1": {
                "Data1": 2710,
                "Data2": 2715
            }
        }
    } {
        "Config1": {
            "TestCase2": {
                "Data1": 2600,
                "Data2": 2900
            }
        },
        "Config2": {
            "TestCase2": {
                "Data1": 2600,
                "Data2": 2900
            }
        }
    }

【问题讨论】:

  • result.update中的dict看起来不对(键上没有引号),json的方法应该是loadsdumps...
  • @bozdoz loads 使用字符串 load 文件对象(或任何实现 read() 的东西)

标签: python json


【解决方案1】:

除了以错误的模式打开文件(应该是'w')之外,您还通过定义一个新的内联字典来覆盖旧的“配置”树。

代替:

result.update({config:{test:{'Data1':2600,'Data2':2900}}})

试试这个:

result[config][test] = {'Data1': 2600, 'Data2': 2900}

这应该会为您提供您正在寻找的示例结果。在您添加 TestCase2 时,它将让 result['Config1']['TestCase1'] 持续存在。如果是None,您可能还需要通过将result[config] 设置为{} 来确保配置树存在。

【讨论】:

    【解决方案2】:

    问题是您在此处将新 JSON 附加到原始 JSON 文件:

    with open('data.json', 'a') as outfile:
        json.dump(result, outfile)
    

    如您所见,您在同一个文件中有两个 JSON 对象:

    ...
                "Data2": 2715
            }
        }
    } {  <--- original object ends here, new object starts here
        "Config1": {
    ...
    

    JSONLint 需要一个对象,任何 JSON 解析器也是如此。

    【讨论】:

      【解决方案3】:

      主要问题是 dict1.update(dict2) 方法会覆盖 dict1 键,如果 dict2 中存在相同的键,因此文件中的第二个对象没有键 =>TestCase1

      另一个问题是(如上所述)文件以错误的模式打开(应该是 w),因为 a 附加到 json 文件中

      你可以试试这个:

      with open("data.json") as json_file:
          json_data = json.load(json_file)
      
      test='TestCase2'
      result=json_data
      myConfigs = ['Config1','Config2']
      for each, config in enumerate(myConfigs):
          result[config].update({test:{'Data1':2600,'Data2':2900}})
      with open('data.json', 'w') as outfile:
          json.dump(result, outfile)
      

      只需result[config].update(... 而不是result.update({config:...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-04-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多