'Export a simple Dictionary into Excel file in python

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.



Solution 1:[1]

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')

Solution 2:[2]

This will write the contents of the dictionary to output.csv. The first column will contain the keys, the second will contain the values.

import csv

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

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Chankey Pathak
Solution 2 Sylvester Kruin