嵌套字典到csv excel [英] Nested dictionary to csv excel

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

问题描述

我已经尝试了许多不同的方法来获得以下结果的excel,

 星期一星期二星期三星期四星期五星期六星期天
早餐0 0 289 0 265 0 0
午餐832 0 0 0 0 0 0
晚餐201 0 0 0 0 0 620

从以下字典:

  my_dict = {Monday:{ 早餐:0,午餐:832,晚餐:201},
星期二:{早餐:0,午餐:0,晚餐:0},
星期三:{早餐:289,午餐:0,晚餐:0},
星期四:{早餐:0,午餐:0,晚餐 0},
星期五:{早餐:265,午餐:0,晚餐:0},
星期六:{早餐:0,午餐 0,晚餐:0},
星期日:{早餐:0,午餐:0,晚餐:620}}
pre>

但我不会ab以获得我想要的结果。我做的一个尝试不行,这是一个:

  import csv 
import itertools

headings =(d.keys()[0] for d in my_dict)
entries =(sorted(my_dict [index] [col] .items())for index,col in
enumerate (标题))

with open('output.csv','wb')as f_output:
csv_output = csv.writer(f_output)
csv_output.writerow([' item'] +标题)

对于colto in itertools.iziplogest(* entries,fillvalue = ['< n / a>'] * len(entries [0])):
csv_output.writerow([cols [0] [0]] + [col [1] for col in cols])


$ b $我很新颖的编程,我还不太熟悉面向对象,所以我想找到一个尽可能简单的答案。谢谢!

解决方案

尝试使用熊猫为此!



安装如果你没有它的包装。

  pip install pandas 

然后执行以下代码:

 将pandas导入pd 
df = pd.DataFrame。 from_dict(data = my_dict)
df = df [['星期一','星期二','星期三','星期四','星期五','星期六','星期日']]
df。 to_csv('output.csv',index = False)

你应该得到一个csv如下: / p>


星期一星期二星期三星期四星期五星期六星期日
早餐0 0 289 0 265 0 0
晚餐201 0 0 0 0 0 620
午餐832 0 0 0 0 0 0



就这样,你应该一个csv,你需要!


I have tried many different approaches to get the below result in excel,

              Monday Tuesday Wednesday Thursday Friday Saturday Sunday
Breakfast     0      0       289       0        265    0         0
Lunch         832    0       0         0        0      0         0
Dinner        201    0       0         0        0      0         620

from the follow dictionary:

 my_dict = {"Monday":{"Breakfast": 0, "Lunch": 832, "Dinner": 201},
       "Tuesday":{"Breakfast": 0, "Lunch": 0, "Dinner": 0},
       "Wednesday":{"Breakfast": 289, "Lunch": 0, "Dinner": 0},
       "Thursday":{"Breakfast": 0, "Lunch": 0, "Dinner": 0},
       "Friday":{"Breakfast": 265, "Lunch": 0, "Dinner": 0},
       "Saturday":{"Breakfast": 0, "Lunch": 0, "Dinner": 0},
       "Sunday":{"Breakfast": 0, "Lunch": 0, "Dinner": 620}}

But I havent be able to get the result I would like. One of the attempts I made that doesnt work is this one:

import csv
import itertools

headings = (d.keys()[0] for d in my_dict)
entries = (sorted(my_dict[index][col].items()) for index, col in 
enumerate(headings))

with open('output.csv', 'wb') as f_output:
    csv_output = csv.writer(f_output)
    csv_output.writerow(['items'] + headings)

    for cols in itertools.iziplogest(*entries, fillvalue=['<n/a>']*len(entries[0])):
         csv_output.writerow([cols[0][0]]+[col[1] for col in cols])

I am new to programming and am not yet overly familiar with object orientation, so I would like to find an answer that is as straight-forward as possible. Thanks!

解决方案

Try using pandas for this!

Install the package if you don't have it already.

pip install pandas 

Then do the following code:

import pandas as pd
df = pd.DataFrame.from_dict(data=my_dict)
df=df[['Monday','Tuesday', 'Wednesday','Thursday','Friday','Saturday','Sunday']]
df.to_csv('output.csv',index=False)

You should get a csv as follows:

Monday Tuesday Wednesday Thursday Friday Saturday Sunday Breakfast 0 0 289 0 265 0 0 Dinner 201 0 0 0 0 0 620 Lunch 832 0 0 0 0 0 0

And that's it , you should have a csv as you require!

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

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