如何用一个键将多个值的字典写入csv文件 [英] how to write a dictionary with one key multiple values to a csv file

查看:123
本文介绍了如何用一个键将多个值的字典写入csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个以下形式的字典:

I have a dictionary of the following form

>>> {'1' : [V3210 , 234567 ,1235675 , 23], '2' : [v3214 , 5678 ,65879 ,89} , ...}

如何将此字典写入csv文件

how to write this dictionary to a csv file??

感谢

推荐答案

使用内置 csv 模块。这是假设您要在键,值1,值2,... 格式。

Use the builtin csv module. This is assuming you want it in a key,value1,value2,... format.

import csv

with open('filename', 'w') as f:
    c = csv.writer(f)

    for key, value in d.items():
        c.writerow([key] + value)

这篇关于如何用一个键将多个值的字典写入csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆