将简单的Dictionary导出为python中的Excel文件

sas*_*ant 4 python dictionary excel-2010

我是python的新手。我有一个简单的字典,其键和值如下

dict1 = {"number of storage arrays": 45, "number of ports":2390,......}
Run Code Online (Sandbox Code Playgroud)

我需要将它们放在excel表格中,如下所示

number of storage arrays 45
number of ports          2390
Run Code Online (Sandbox Code Playgroud)

我有一本很大的字典。

Cha*_*hak 18

你可以使用熊猫。

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')
Run Code Online (Sandbox Code Playgroud)


Dan*_*ake 6

萨西坎特

这将打开一个名为的文件,并将output.csv字典的内容输出到电子表格中。第一列将具有键,第二列将具有值。

import csv

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

您可以使用excel打开csv,并将其保存为所需的任何格式。

  • 绝妙的主意,应该在 py3 上使用 `dict1.items()` (2认同)