【问题标题】:replace value and rebuild the json data python替换值并重建json数据python
【发布时间】:2020-06-12 07:08:16
【问题描述】:

我有一个场景,我有以下 json 数据

 data = {
      "app": [
        {
          "ida": 10,
          "configur": {
            "config": [
              {
                "active": "true",
                "tol": "id"
              }
            ]
          }
        },
        {
          "ida": 11,
          "configur": {
            "config": [
              {
                "active": "true",
                "tol": "id"
              }
            ]
          }
        },    {
          "ida": 11,
          "configur": {
            "config": [
              {
                "active": "true",
                "tol": "id"
              }
            ]
          }
        }
      ]
    }

这里我想将每个元素中的tol 值替换为ida

这是我的代码:

  data3 = []
  for cmpkey2 in data['app']:
        spcmpg = cmpkey2['configur']['config']
        tempev = cmpkey2['ida']
        for key34 in spcmpg:
            key34['tol'] = tempev
        data3.append({ "ida": tempev, "config": spcmpg})

这里的新字典 data3 没有用 ida 值替换 tol 值,我在哪里做错了? 任何帮助都会很棒!

【问题讨论】:

    标签: python json python-3.x list


    【解决方案1】:

    如果是字典,您不能更改键的名称,您可以删除该键或创建一个新键

    我找到了解决您问题的方法,

    data = {
          "app": [
            {
              "ida": 10,
              "configur": {
                "config": [
                  {
                    "active": "true",
                    "tol": "id"
                  }
                ]
              }
            },
            {
              "ida": 11,
              "configur": {
                "config": [
                  {
                    "active": "true",
                    "tol": "id"
                  }
                ]
              }
            },    {
              "ida": 11,
              "configur": {
                "config": [
                  {
                    "active": "true",
                    "tol": "id"
                  }
                ]
              }
            }
          ]
        }
    
    val=''
    data3 = []
    for cmpkey2 in data['app']:
        spcmpg = cmpkey2['configur']['config']
    
        tempev = cmpkey2['ida']
        for key34 in spcmpg:
    
            val=cmpkey2['ida']
            key34['tol']=val
    
    
        data3.append({ "ida": tempev, "config": spcmpg})
    print(data3)
    

    在此代码中,它创建一个新键并将 tol 的值分配给该键,然后删除 tol

    输出:

    [{'ida': 10, 'config': [{'active': 'true', 'tol': 10}]}, {'ida': 11, 'config': [{'active': 'true', 'tol': 11}]}, {'ida': 11, 'config': [{'active': 'true', 'tol': 11}]}]
    

    【讨论】:

    • 我正在寻求替换 tol 的值 id 所以 id 值应该替换为 ida
    • 我已经编辑了我的答案,看看它,如果有问题请告诉我
    • 不工作只取id 11 的最后一个值并附加到所有其他tol 值,所以我得到的输出是[{'ida': 10, 'config': [{'active': 'true', 'tol': 11}]}, {'ida': 11, 'config': [{'active': 'true', 'tol': 11}]}, {'ida': 11, 'config': [{'active': 'true', 'tol': 11}]}]
    【解决方案2】:

    你能试试下面的代码,看看你是否期望你的输出是这样的

    data3 = []
    
    for item in data['app']:
        new_dict = {}
        config_list = []
        ida_value = item['ida']
        config_list = item['configur']['config']
        config_list[0]['tol'] = ida_value
        new_dict['ida'] = ida_value
        new_dict['config'] = config_list
        data3.append(new_dict)
    print(data3)
    

    输出

    [
       {
          "ida":10,
          "config":[
             {
                "active":"true",
                "tol":10
             }
          ]
       },
       {
          "ida":11,
          "config":[
             {
                "active":"true",
                "tol":11
             }
          ]
       },
       {
          "ida":11,
          "config":[
             {
                "active":"true",
                "tol":11
             }
          ]
       }
    ]
    

    【讨论】:

    • 我不知道为什么会这样,但我仍然得到这个输出[{'ida': 10, 'config': [{'active': 'true', 'tol': 11}]}, {'ida': 11, 'config': [{'active': 'true', 'tol': 11}]}, {'ida': 11, 'config': [{'active': 'true', 'tol': 11}]}] 最后一个元素附加在所有tol 值中
    • 这是一个循环问题,因为我看到只有最后一个元素值被保存
    • 你使用了我提供的代码吗?还是到处都有 11 个?
    • @John 根据我上面写的代码,我得到了提供的输出,这就是你所期望的。不太确定使用我编写的相同代码如何获得不同的响应。
    猜你喜欢
    • 1970-01-01
    • 2018-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-15
    相关资源
    最近更新 更多