【问题标题】:How to delete values (in my case: 'phones') from json file如何从 json 文件中删除值(在我的例子中:'phones')
【发布时间】:2020-06-07 20:17:12
【问题描述】:

我是 json 文件语法的新手,我想知道如何从我的 json 文件中删除“phones”及其值,以便 end、start、alignedWord 和 word 是我在 words 键中的唯一值。

我的代码:

with open('dest_file.json', 'w') as dest_file:
    with open('test1.json', 'r') as j:
        contents = json.loads(j.read())
        for value in contents['words']:
            del(value['case'])
            del(value['endOffset'])
            del(value['startOffset'])
        dest_file.write(json.dumps(contents,indent = 4))

示例 JSON 对象:

"words": [
        {
            "alignedWord": "the",
            "end": 6.31,
            "phones": [
                {
                    "duration": 0.09,
                    "phone": "dh_B"
                },
                {
                    "duration": 0.05,
                    "phone": "iy_E"
                }
            ],
            "start": 6.17,
            "word": "The"
        },

除此之外,手机究竟是什么数据类型,为什么我无法通过我当前的代码删除它?

【问题讨论】:

    标签: python json with-statement del


    【解决方案1】:

    在 JSON 元素中,您可以像数组/列表或字典一样操作数据。在这种情况下,您可以使用value.pop("phones")

    例如

    import json
    
    data = '''
    [
    {
        "alignedWord": "the",
        "end": 6.31,
        "phones": [
            {
                "duration": 0.09,
                "phone": "dh_B"
            },
            {
                "duration": 0.05,
                "phone": "iy_E"
            }
        ],
        "start": 6.17,
        "word": "The"
    }]'''
    
    contents = json.loads(data)
    for value in contents:
      value.pop('phones')
    
    print(json.dumps(contents))
    

    查看运行:https://repl.it/repls/DarkgreyLightcoralInstitution

    【讨论】:

    • 运行你的代码行我得到错误: Traceback(最近一次调用最后):文件“jsonIterator.py”,第 35 行,在 value.pop("phones") KeyError: '手机的
    【解决方案2】:

    试试这个:

    如果要将编辑后的 ​​json 保存到新文件中:

    import json
    
    with open('dest_file.json', 'w') as dest_file:
        with open('source_file.json', 'r') as source_file:
            for line in source_file:
                element = json.loads(line.strip())
                if 'phones' in element:
                    del element['phones']
                dest_file.write(json.dumps(element))
    

    如果你想在编辑后重新写入相同的json:

    import json
    
    with open('dest_file.json') as data_file:
        data = json.load(data_file)
    
    for element in data:
        if 'phones' in element:
            del element['phones']
    
    with open('dest_file.json', 'w') as data_file:
        data = json.dump(data, data_file)
    

    【讨论】:

      【解决方案3】:

      "phones" 在上述情况下将是一个字典列表。 “单词”本身也是字典列表本身。您需要先访问列表中的项目,然后才能按照上面的建议跳转到字典弹出窗口。

      words =[
              {
                  "alignedWord": "the",
                  "end": 6.31,
                  "phones": [
                      {
                          "duration": 0.09,
                          "phone": "dh_B"
                      },
                      {
                          "duration": 0.05,
                          "phone": "iy_E"
                      }
                  ],
                  "start": 6.17,
                  "word": "The"
              }
      ]
      

      显示单个元素:

      words[0]['phones']
      

      删除:

      words[0].pop('phones')
      

      您可以按照建议使用上面的代码进行迭代和弹出!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-09-18
        • 1970-01-01
        • 2019-09-15
        • 1970-01-01
        • 1970-01-01
        • 2022-11-16
        • 1970-01-01
        相关资源
        最近更新 更多