Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 913
sasikant Avatar asked Feb 17 '15 05:02

sasikant


People also ask

How do I export a dictionary to Excel?

In the explorer, open the dictionary. Click Import / Export > Export Excel. The option Export the entire dictionary is selected by default.

How do I export data from Python to Excel?

to_excel() Function in Python. If we want to write tabular data to an Excel sheet in Python, we can use the to_excel() function in Pandas DataFrame . A pandas DataFrame is a data structure that stores tabular data. The to_excel() function takes two input parameters: the file's name and the sheet's name.

How do I save a dictionary as a csv file?

In Python to convert a dictionary to CSV use the dictwriter() method. This method is used to insert data into the CSV file. In Python, the CSV module stores the dictwriter() method. It creates an object and works like the dictwriter().


1 Answers

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.

like image 56
Daniel Timberlake Avatar answered Oct 30 '22 09:10

Daniel Timberlake