【问题标题】:Parse nested JSON file, take out specific attributes and create .txt files out of it解析嵌套的 JSON 文件,取出特定属性并从中创建 .txt 文件
【发布时间】:2021-12-10 22:34:52
【问题描述】:

所以我在这里有一个大的 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",
        }
    }
}

然后我只过滤具有“供应商”==“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

【问题讨论】:

    标签: python json attributes txt


    【解决方案1】:

    所以终于设法让这个工作,这里的代码可能有一天会帮助某人:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-06
      • 2017-04-17
      • 1970-01-01
      相关资源
      最近更新 更多