简体   繁体   English

将带有列表的字典转换为csv文件,其中列为键,值在行中

[英]convert dictionary with lists to csv file where the columns are the keys and the values are in the rows

Hi i have a dictionary which contains indicator of comprise it looks like that: 嗨,我有一本字典,其中包含包含它的指示符,如下所示:

{'url' : [ 'malware.com ', 'hack.net ', 'paypalz.org' ] 'IP' ['104.223.89.166' , 104.223.89.136] 'emailIdentifier' : ['liler@bikurcholim.net']}

i need to convert it to csv where the keys are the columns and the values are in rows. 我需要将其转换为csv,其中键是列,值是行。 i tried to find solutions in stackoverflow flow to this case but i didn't find one. 我试图在这种情况下的stackoverflow流中找到解决方案,但我没有找到解决方案。 any syggestions how to do it ? 任何糖尿怎么办?

thank you 谢谢

with open('\\cybernet\\cyber_indicators.csv', 'w') as csv_file:

    writer = csv.writer(csv_file)

    dict_writer = csv.DictWriter(csv_file, keys)

    for key, value in dictionary.items():

        writer.writerow([key, value])
import csv
dictionary={'url' : [ 'malware.com ', 'hack.net ', 'paypalz.org' ] ,'IP' : ['104.223.89.166' , '104.223.89.136'] ,'emailIdentifier' : ['liler@bikurcholim.net']}
list_temp=[]
with open('data.csv','w') as csv_file:
    writer = csv.writer(csv_file)
    writer.writerow(dictionary.keys())
    for key, value in dictionary.items():
        list_temp.append(value)
    list_last=[x for x in dictionary.values()]
    max_length=0
    for item in list_last:
        if(len(item)>max_length):
            max_length=len(item)
    for item in list_last:
        append_count=max_length-len(item)
        for i in range(append_count):
            item.append('')
    zipped_list=list_last=set(zip(*list_temp))
    for item in zipped_list:
        writer.writerow(item)

data.csv data.csv

url,IP,emailIdentifier
malware.com ,104.223.89.166,liler@bikurcholim.net
paypalz.org,,
hack.net ,104.223.89.136,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM