【问题标题】:Python String to FilePython 字符串到文件
【发布时间】:2023-03-08 05:55:01
【问题描述】:

我正在使用一个名为 PyAttck 的模块来提取数据,并且我有以下代码:

from pyattck import Attck

attack = Attck()

for technique in attack.enterprise.techniques:
    print(technique.id)
    print(technique.name)
    for subtechnique in technique.subtechniques:
        print(subtechnique.id)
        print(subtechnique.name)

这使我可以打印数据并查看它们中的每一个的类型,它们都是 。有没有办法可以将这些导出到 CSV 或 JSON 文件中?

【问题讨论】:

    标签: python json pandas string


    【解决方案1】:

    首先我会展平数据,然后使用一个简单的函数写入文件:

    # your data structure
    attck_data = [
        {'id': '0', 'name': 'technique1', 'subtechniques': [
            {'id': '0', 'name': 'subtechnique0'},
            {'id': '1', 'name': 'subtechnique1'},
            {'id': '2', 'name': 'subtechnique2'},
        ]},
        {'id': '1', 'name': 'technique2', 'subtechniques': [
            {'id': '3', 'name': 'subtechnique3'},
            {'id': '4', 'name': 'subtechnique4'},
        ]},
    ]
    
    # list comprehension to shape the data into a usable format
    attck_flat = [{'id': technique['id'],
                   'name': technique['name'],
                   'sub_name': subtechnique['name'],
                   'sub_id': subtechnique['id']}
                  for technique in attck_data
                  for subtechnique in technique['subtechniques']]
    
    # let's see the new data
    from pprint import pprint
    pprint(attck_flat)
    

    注意:我使用了一个字典列表来假设 Attck 类的数据结构,因为你拥有它的方式是不可复制的,即 technique.id 而不是 technique['id']

    您的代码将如下所示:

    attck_flat = [{'id': technique.id,
                   'name': technique.name,
                   'sub_name': subtechnique.name,
                   'sub_id': subtechnique.id}
                  for technique in attack.enterprise.techniques
                  for subtechnique in technique.subtechniques]
    

    输出:

    [{'id': '0', 'name': 'technique1', 'sub_id': '0', 'sub_name': 'subtechnique0'},
     {'id': '0', 'name': 'technique1', 'sub_id': '1', 'sub_name': 'subtechnique1'},
     {'id': '0', 'name': 'technique1', 'sub_id': '2', 'sub_name': 'subtechnique2'},
     {'id': '1', 'name': 'technique2', 'sub_id': '3', 'sub_name': 'subtechnique3'},
     {'id': '1', 'name': 'technique2', 'sub_id': '4', 'sub_name': 'subtechnique4'}]
    

    从这里你可以使用我常用的 json/csv 编写器之一

    json:

    # funciton to write json files
    import json
    def write_json(data, path: str, indent=4):
        with open(path, 'w') as file:
            json.dump(data, file, indent=indent)
    
    write_json(attck_flat, './attck.json')
    

    输出:

    [
        {
            "id": "0",
            "name": "technique1",
            "sub_name": "subtechnique0",
            "sub_id": "0"
        },
        {
            "id": "0",
            "name": "technique1",
            "sub_name": "subtechnique1",
            "sub_id": "1"
        },
        {
            "id": "0",
            "name": "technique1",
            "sub_name": "subtechnique2",
            "sub_id": "2"
        },
        {
            "id": "1",
            "name": "technique2",
            "sub_name": "subtechnique3",
            "sub_id": "3"
        },
        {
            "id": "1",
            "name": "technique2",
            "sub_name": "subtechnique4",
            "sub_id": "4"
        }
    ]
    

    csv:

    # function to write csv files
    import csv
    def write_csv(data, path: str):
        with open(path, 'w') as file:
            # get all the keys
            fieldnames = set().union(*data)
            writer = csv.DictWriter(file, fieldnames=fieldnames,
                                    lineterminator='\n')
            writer.writeheader()
            writer.writerows(data)
    
    write_csv(attck_flat, './attck.csv')
    

    输出:

    sub_name,name,id,sub_id
    subtechnique0,technique1,0,0
    subtechnique1,technique1,0,1
    subtechnique2,technique1,0,2
    subtechnique3,technique2,1,3
    subtechnique4,technique2,1,4
    

    【讨论】:

      猜你喜欢
      • 2020-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多