嗨,我有一本字典,其中包含构成指示符,如下所示:
{'url' : [ 'malware.com ', 'hack.net ', 'paypalz.org' ] 'IP' ['104.223.89.166' , 104.223.89.136] 'emailIdentifier' : ['liler@bikurcholim.net']}
我需要将其转换为csv,其中键是列,值是行。我试图在这种情况下的stackoverflow流中找到解决方案,但我没有找到解决方案。任何糖浆怎么办?
谢谢
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])
答案 0 :(得分:1)
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
url,IP,emailIdentifier
malware.com ,104.223.89.166,liler@bikurcholim.net
paypalz.org,,
hack.net ,104.223.89.136,