Python字符串到文件

我正在使用一个名为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)

这使我可以打印数据并查看每个数据的类型,它们都是<class'str'>。有什么方法可以将它们导出到CSV或JSON文件吗?

顽强的B

首先,我将整理数据,然后使用一个简单的函数写入文件:

# 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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

读取文件到字符串(python)

来自分类Dev

python中字符串到文件路径的问题

来自分类Dev

python中文件到字符串的转换

来自分类Dev

JavaScript文件到字符串到文件到URL

来自分类Dev

RTF文件到属性字符串

来自分类Dev

文件写入:字符串到字节

来自分类Dev

Android文件到字符串

来自分类Dev

文件写入:字符串到字节

来自分类Dev

文件到字符串数组(逐行)

来自分类Dev

Python unicode字符串到字符串?

来自分类Dev

从C返回字符串到Python

来自分类Dev

Python字符串到模块参考

来自分类Dev

Python字典到字符串

来自分类Dev

python字符串到字典

来自分类Dev

从CSV到列表的字符串-Python

来自分类Dev

从C返回字符串到Python

来自分类Dev

到元组对的字符串的Python列表

来自分类Dev

字符串到图片 - python

来自分类Dev

字符串到短语替换python

来自分类Dev

Python 字符串到数组的转换

来自分类Dev

从文件中获取这些字符串时,字符串到文件中的字符串替换

来自分类Dev

未在Python 3中显式打开文件时处理从字节到字符串的转换

来自分类Dev

读取csv文件到python ValueError:无法将字符串转换为float

来自分类Dev

Python正则表达式字符串值到文件末尾

来自分类Dev

将字符串从Python正则表达式存储到文件列表中

来自分类Dev

未在Python 3中显式打开文件时处理从字节到字符串的转换

来自分类Dev

字符串到字符串[]

来自分类Dev

python从文件中拆分字符串

来自分类Dev

Python:将字符串与文件合并