首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >文件的Python字符串

文件的Python字符串
EN

Stack Overflow用户
提问于 2021-02-03 00:30:49
回答 1查看 39关注 0票数 0

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

代码语言:javascript
复制
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文件?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-03 09:15:24

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

代码语言:javascript
复制
# 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']

您的代码将如下所示:

代码语言:javascript
复制
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]

输出:

代码语言:javascript
复制
[{'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:

代码语言:javascript
复制
# 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')

输出:

代码语言:javascript
复制
[
    {
        "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:

代码语言:javascript
复制
# 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')

输出:

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66013749

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档