【问题标题】:Given a string inside a nested Json, how can I change its id given a specific string?给定嵌套 Json 中的字符串,如何在给定特定字符串的情况下更改其 id?
【发布时间】:2018-10-06 11:03:18
【问题描述】:

我正在阅读一个 Json,它是这样的几个字典的列表:

a_list = [ {
    "ID": "20394820938",
    "data": [
      {
        "fruit": "tomato",
        "provider": "walmart",
        "availability": "True",
        "Type": false
      },
      {
        "fruit": " orange ",
        "Type": false
      },
      {
        "fruit": "watermelon",
        "provider": "destination",
        "availability": "cotsco",
        "Type": false
      },
      {
        "fruit": " the watermelon is new",
        "Type": false
      }
    ],
    "API": false,
    "count": 0
  },  
  {
    "ID": "203948lHBFNPO",
    "data": [
      {
        "fruit": "apple",
        "provider": "walmart",
        "availability": "True",
        "Type": false
      },
      {
        "fruit": " this is a tomato ",
        "Type": false
      },
      {
        "fruit": "lemon",
        "provider": "unknown",
        "availability": "cotsco",
        "Type": false
      },
      {
        "fruit": " orange ",
        "Type": false
      },
      {
        "fruit": "banana",
        "provider": "unknown",
        "availability": "unknown",
        "Type": false
      }
    ],
    "API": false,
  }
]

当且仅当字典在其子元素之一中包含字符串 apple 时,我如何将 IM00N 附加到 Json 的 id 值?例如:

a_list = [ {
    "ID": "20394820938",
    "data": [
      {
        "fruit": "tomato",
        "provider": "walmart",
        "availability": "True",
        "Type": false
      },
      {
        "fruit": "apple",
        "Type": false
      },
      {
        "fruit": "watermelon",
        "provider": "destination",
        "availability": "cotsco",
        "Type": false
      },
      {
        "fruit": " the watermelon is new",
        "Type": false
      }
    ],
    "API": false,
    "count": 0
  },  
  {
    "ID": "IM00N-203948lHBFNPO",
    "data": [
      {
        "fruit": "apple",
        "provider": "walmart",
        "availability": "True",
        "Type": false
      },
      {
        "fruit": " this is a tomato ",
        "Type": false
      },
      {
        "fruit": "lemon",
        "provider": "unknown",
        "availability": "cotsco",
        "Type": false
      },
      {
        "fruit": " orange ",
        "Type": false
      },
      {
        "fruit": "banana",
        "provider": "unknown",
        "availability": "unknown",
        "Type": false
      }
    ],
    "API": false,
  }
]

我尝试构建一个正则表达式,但它很困难,因为列表中每个子词典的元素中没有模式。处理这种匹配和替换的正确方法是什么?

【问题讨论】:

    标签: python json string dictionary


    【解决方案1】:

    您可以使用 for 循环修改数据:

    for entry in a_list:
        if any('apple' in item['fruit'] for item in entry['data']):
            entry['ID'] = 'IM00N-' + entry['ID']
    

    如果你的数据的形状不同,以至于某些条目没有fruit键,你可以捕捉KeyError或使用dict.get

    for entry in a_list:
        if any('apple' in item.get('fruit', '') for item in entry.get('data', [])):
            entry['ID'] = 'IM00N-' + entry['ID']
    

    【讨论】:

    • 我已经更新了答案。您可以将in 运算符与字符串一起使用。
    • 你能展示一下try catch修改吗?感谢您的帮助!
    • 在生成器表达式中,不能使用try,但可以使用dict.get 代替默认值。
    猜你喜欢
    • 1970-01-01
    • 2019-12-07
    • 1970-01-01
    • 2012-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    • 1970-01-01
    相关资源
    最近更新 更多