[英]Write Python dictionary to CSV where where keys= columns, values = rows
[英]convert dictionary with lists to csv file where the columns are the keys and the values are in the rows
嗨,我有一本字典,其中包含包含它的指示符,如下所示:
{'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])
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,
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.