将带列表的词典转换为csv文件,其中列是键,值是行

2024-09-30 18:26:55 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,我有一本字典,里面有一个索引,看起来是这样的:

{'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])

Tags: csvkeyorgipcomurlnet字典
1条回答
网友
1楼 · 发布于 2024-09-30 18:26:55
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)

你知道吗数据.csv你知道吗

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

相关问题 更多 >