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

2024-10-01 19:30:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我这里有一个大的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

Tags: pathintxtfordatacomponentsitemscomp
1条回答
网友
1楼 · 发布于 2024-10-01 19:30:59

因此,最终我们成功地实现了这一目标,以下代码可能会在某一天对某些人有所帮助:

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()

相关问题 更多 >

    热门问题