在python中将字典字典写入csv [英] Write a dictionary of dictionaries to csv in python

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

问题描述

我有一本字典,我想写出来的csv。我的字典看起来像:

  dict = {
object1:
{
time1: value1,value2],
time2:[value3,value4]
},
object2:
{
time1:[value5,value6],time2 [value7,value8 ]
}
}

我想写成: / p>

object1



time1,value1,value2



time2,value3,value4



object2



time1,value5,value6



time2,value7,value8



到目前为止,使用以下代码将键/值对写出csv:

  writer = csv.writer(open('test.csv' ,'wb'))
for key,value Dict.items():
writer.writerow([key,value])

所以它看起来像:

  objcet1,{
time1:[value1,value2],time2:[value3,value4]
}

objcet2,{
time1:[value5,value6]
}

我试着只写外键(key1和object2)

  writer = csv.writer(open('test.csv','wb'))
Dict.key()中的值:
writer.writerow([key])

它只是写出一个空白文件。我想如果我能想出如何写自己的外键,我会能够写出csv如我想要的。有人有任何建议吗?

解决方案

我个人建议使用Excel,我一直在使用.csv文件, .xlsx。

  import xlsxwriter 
workbook = xlsxwriter.Workbook('File.xlsx')
工作表= workbook.add_worksheet()
row = 0
col = 0
for键in dictionary.keys():
row + = 1
worksheet.write ,col,key)
for item in dictionary [key]:
worksheet.write(row,col + 1,item)
worksheet.write(row,col + 2,dictionary [key ] [item] [0])
row + = 1

workbook.close()

在那里你可以改变列和行增量,以便你得到你想要的excel表。
您甚至可以从Excel导出为.csv文件。


I have a dictionary of dictionaries that I would like to write out to csv. my dictionary looks like:

dict={
    object1:
        {
            time1:[value1,value2],
            time2:[value3,value4]
        },
    object2:
        {
            time1:[value5,value6],time2[value7,value8]
        }
    }

I would like it to write out as:

object1

time1, value1, value2

time2, value3, value4

object2

time1, value5, value6

time2, value7, value8

Thus far, I can only figure out how to write the key/value pairs out csv using the following code:

writer = csv.writer(open('test.csv','wb'))
for key, value in Dict.items():
    writer.writerow([key,value])

So it looks like:

objcet1,{
    time1:[value1,value2],time2:[value3,value4]
}

objcet2,{
    time1:[value5,value6],time2:[value7,value8]
}

I have tried writing just the outer keys (keys labeled object1 and object2) using

writer = csv.writer(open('test.csv','wb'))
for key, value in Dict.key():
    writer.writerow([key])

but it just writes out a blank file. I think If I could figure out how to write the outer keys by themselves, I would be able to write out the csv as I want. Does anyone have any suggestions?

解决方案

I personally recommend using Excel, I have been working on .csv files, but ended up using .xlsx.

import xlsxwriter
workbook = xlsxwriter.Workbook('File.xlsx')
worksheet = workbook.add_worksheet()                            
row = 0
col = 0
for key in dictionary.keys():
    row += 1
    worksheet.write(row, col, key)
    for item in dictionary[key]:
        worksheet.write(row, col + 1, item)
        worksheet.write(row, col + 2, dictionary[key][item][0])
        row += 1

workbook.close()

And there you can change the columns and the rows increment so that you get the excel table you want to. You can even export it as a .csv file afterwards from Excel.

这篇关于在python中将字典字典写入csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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