Export a simple Dictionary into Excel file in python

感情迁移 提交于 2019-12-05 02:00:07

问题


I am new to python. I have a simple dictionary for which the key and values are as follows

dict1 = {"number of storage arrays": 45, "number of ports":2390,......}

i need to get them in a excel sheet as follows

number of storage arrays 45
number of ports          2390

I have a very big dictionary.


回答1:


Sassikant,

This will open a file named output.csv and output the contents of your dictionary into a spreadsheet. The first column will have the key, the second the value.

import csv

with open('output.csv', 'wb') as output:
    writer = csv.writer(output)
    for key, value in dict1.iteritems():
        writer.writerow([key, value])

You can open the csv with excel and save it to any format you'd like.




回答2:


You can use pandas.

import pandas as pd

dict1 = {"number of storage arrays": 45, "number of ports":2390}

df = pd.DataFrame(data=dict1, index=[0])

df = (df.T)

print (df)

df.to_excel('dict1.xlsx')


来源:https://stackoverflow.com/questions/28555112/export-a-simple-dictionary-into-excel-file-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!