python 基于字符串列表移除JSON列表字符串项

66bbxpm5  于 2023-01-29  发布在  Python
关注(0)|答案(2)|浏览(158)

下面是我的示例json文件:

{
        "test": [{
            "Location": "Singapore",
            "Values": [{
                    "Name": "00",
                    "subvalues": [
                        "5782115e1",
                        "688ddd16e",
                        "3e91dc966",
                        "5add96256",
                        "10352cf0f",
                        "17f233d31",
                        "130c09135",
                        "2f49eb2a6",
                        "2ae1ad9e0",
                        "23fd76115"
                    ]
                },
                {
                    "Name": "01",
                    "subvalues": [
                        "b43678dfe",
                        "202c7f508",
                        "73afcaf7c"
                    ]
                }
            ]
        }]
    }

我尝试使用以下列表从json文件中删除:["130c09135"、"2f49eb2a6"、"5782115e1"、"b43678dfe"]
最终结果:

{
    "test": [{
        "Location": "Singapore",
        "Values": [{
                "Name": "00",
                "subvalues": [
                    "688ddd16e",
                    "3e91dc966",
                    "5add96256",
                    "10352cf0f",
                    "17f233d31",
                    "2ae1ad9e0",
                    "23fd76115"
                ]
            },
            {
                "Name": "01",
                "subvalues": [
                    "202c7f508",
                    "73afcaf7c"
                ]
            }
        ]
    }]
 }

我知道在文本中使用replace会破坏结构,这是json的新手,如果有任何帮助,我将不胜感激。

ql3eal8s

ql3eal8s1#

这里是一个不依赖于键或深度的名称的通用方法,唯一的假设是如果字典包含任何完全由字符串组成的列表,它将被重构 * 排除 * 某些值--即EXCLUSIONS集合

from json import load as LOAD, dumps as DUMPS

FILENAME = '/Volumes/G-Drive/foo.json'
EXCLUSIONS = {"130c09135", "2f49eb2a6", "5782115e1", "b43678dfe"}

def process(d):
    if isinstance(d, dict):
        for v in d.values():
            process(v)
    elif isinstance(d, list):
        if all(isinstance(v, str) for v in d):
            d[:] = [v for v in d if v not in EXCLUSIONS]
        else:
            for v in d:
                process(v)
    return d

with open(FILENAME) as data:
    print(DUMPS(process(LOAD(data)), indent=2))
    • 输出:**
{
  "test": [
    {
      "Location": "Singapore",
      "Values": [
        {
          "Name": "00",
          "Subvalues": [
            "688ddd16e",
            "3e91dc966",
            "5add96256",
            "10352cf0f",
            "17f233d31",
            "2ae1ad9e0",
            "23fd76115"
          ]
        },
        {
          "Name": "01",
          "subvalues": [
            "202c7f508",
            "73afcaf7c"
          ]
        }
      ]
    }
  ]
}
f8rj6qna

f8rj6qna2#

您可以使用以下代码片段:

import json

toRemoveList = ["130c09135", "2f49eb2a6", "5782115e1", "b43678dfe"]

with open('data.json', 'r') as file:
    jsonData = json.loads(file.read())

for valueIndex in range(0, len(jsonData["test"][0]["Values"])):
    value = jsonData["test"][0]["Values"][valueIndex]

    filtered = [x for x in value["Subvalues"] if x not in toRemoveList]

    jsonData["test"][0]["Values"][valueIndex]["Subvalues"] = filtered

with open('newData.json', 'w') as file:
    json.dump(jsonData, file, indent=4)

注意:在每个示例中,您必须使用相同写法的“Subvalues”。不能在不同示例中使用“Subvalues”和“subvalues ...”

相关问题