解析嵌套 JSON 文件,取出特定属性并从中创建 .txt 文件

Parse nested JSON file, take out specific attributes and create .txt files out of it

所以我这里有一个大 JSON 文件,如下所示:

data = {
    "Module1": {
        "Description": "",
        "Layer": "1",
        "SourceDir": "pathModule1",
        "Attributes": {
            "some",
        },
        "Vendor": "comp",
        "components":{
            "Component1": {
               "path": "pathToCom1",
               "includes": [
                   "include1",
                   "include2",
                   "include3",
                   "include4",
                   "include5"
               ]
               "generated:" "txt"
               "memory:" "txt"
               etc
            },
            "Component2":{
               "path": "pathToCom2",
               "includes": [
                   "include1",
                   "include2",
                   "include3",
                   "include4",
                   "include5"
               ]
               "generated:" "txt"
               "memory:" "txt"
               etc
            }
        }
    },
    "Module2": {
        "Description": "",
        "Layer": "2",
        "SourceDir": "pathModule2",
        "Attributes": {
            "some",
        },
        "Vendor": "comp",
        "components":{
            "Component1": {
               "path": "pathToCom1",
               "includes": [
                   "include1",
                   "include2",
                   "include3",
                   "include4",
                   "include5"
               ]
               "generated:" "txt"
               "memory:" "txt"
               etc
            },
            "Component2":{
               "path": "pathToCom2",
               "includes": [
                   "include1",
                   "include2",
                   "include3",
                   "include4",
                   "include5"
               ]
               "generated:" "txt"
               "memory:" "txt"
               etc
            }
        }
    },
    "Module3": {
        "Description": "",
        "Layer": "3",
        "SourceDir": "path",
        "Attributes": {
            "some",
        },
        "Vendor": "",
    },
    "Module4": {
        "Description": "",
        "Layer": "4",
        "SourceDir": "path",
        "Attributes": {
            "some",
        }
    }
}

然后我只过滤字段为“Vendor”==“comp”的那些:

data = {k: v for k,v in data.items() if v.get("Vendor") == "comp"}

然后过滤掉并得到最终输出:

Module1 pathModule1 [('Component1', 'pathToCom1', ['include1', 'include2', 'include3', 'include4', 'include5']), ('Component2', 'pathToCom2', ['include1', 'include2', 'include3', 'include4', 'include5'])]
Module2 pathModule2 [('Component1', 'pathToCom1', ['include1', 'include2', 'include3', 'include4', 'include5']), ('Component2', 'pathToCom2', ['include1', 'include2', 'include3', 'include4', 'include5'])]

代码:

for k,v in data2.items():
    components = [(comp_name, comp_data["path"], comp_data["includes"]) for comp_name, comp_data in v["components"].items()]
    print(k, v["SourceDir"], components)

现在我要做的下一件事,作为最终输出 -> 在文件夹中创建一些 .txt 文件,这些文件将被命名为模块名称,并包含其组件的路径,如下所示:

Module1.txt 应该只包含其组件的路径,所以

Module1.txt has inside:
pathToCom1
pathToCom2

Module2.txt with:
pathToCom1
pathToCom2

而且,includes 应该存储在相应的 .txt 文件中,所以我们会在组件的末尾有名称,它是“includes”字段,所以我们会:

Component1.txt with inside:
 include1
 include2
 include3
 include4
 include5

Component2.txt with inside:
 include1
 include2
 include3
 include4
 include5

编辑:

所以我设法得到了这个,代码是:

for k,v in data.items():
    components = [(comp_name, comp_data["path"], comp_data["includes"]) for comp_name, comp_data in v["components"].items()]
    with open(components_path+k+'.txt', 'w') as f:
        for i,n in v['components '].items():
            path_to_write = n['path'] 
            f.write(path_to_write+'\n')
        f.close()
        for i,n in v['components'].items():
            with open(path_to_includes+i+'.txt', 'w') as f:
                includes_to_write = n['includes'] 
                f.write(str(includes_to_write)+'\n')
            f.close()

现在唯一的问题是我得到了一行:

['include1', 'include2', 'include3', 'include4'..]

I need them to be:
 include1
 include2
 include3
 include4
 include5

所以终于成功了,这里的代码可能会在某天对某人有所帮助:

for k,v in data.items():
    components = [(comp_name, comp_data["path"], comp_data["includes"]) for comp_name, comp_data in v["components"].items()]
    with open(components_path+k+'.txt', 'w') as f:
        for i,n in v['components '].items():
            path_to_write = n['path'] 
            f.write(path_to_write+'\n')
        f.close()
        for i,n in v['components'].items():
            with open(path_to_includes+i+'.txt', 'w') as f:
                includes_to_write = n['includes'] 
                for line in includes_to_write:
                    f.write(line+'\n')
            f.close()