Python Dictionaries to CSV with examples

In this Python Tutorial, we will discuss the Python Dictionaries to CSV (Comma-Separated-Values)  with some examples.

Python Dictionary to CSV

Here, we first need to understand the CSV(Comma-Separated Values).

  • CSV(Comma Separated Values) 

This file has an extension (.csv). It is also a popular file format that is widely supported by various platforms and applications for different uses.

Python uses the dictionary writer method to convert dictionary data into CSV files because we use the dictionary writer(). The CSV module contains a method for working with data.

Take some examples to understand them in a better way:-

  • Python Dictionary to CSV for Simple Interest

First, let’s take a look at the Python CSV module and import it to create a CSV file from this data.

import csv
Interest_info = ['S_No','P','N','R','S_I']

Create a dictionary of data on principal, interest rate, and years of investment, look up data on simple interest, and create a CSV file according to all given data. We take 5 sample input data to get the Simple Interest from them.

n_dict = [                                                
{'S_No':1, 'P':10000 , 'N': 1.0, 'R': 0.5 , 'S_I': 50},           
{'S_No':2, 'P':12000 , 'N': 2.0, 'R': 1.0 , 'S_I': 240},            
{'S_No':3, 'P':14000 , 'N': 2.5, 'R': 1.5 , 'S_I': 525},
{'S_No':4, 'P':16000 , 'N': 3.0, 'R': 2.0 , 'S_I': 960},
{'S_No':5, 'P':18000 , 'N': 4.0, 'R': 2.5 , 'S_I': 1800},
]

Since we want to open the file in writing format, we can use the open() function. In this example, the row() writer function is here to write the row argument.

Python Dictionary to CSV File – Complete code in Python(3.7)

                 # Python Dictionary to CSV(Comma-separated-Values) 
                 
import csv
Interest_info = ['S_No','P','N','R','S_I']
                                                           # P = Principal/Amount
n_dict = [                                               # N = Number of Year's
{'S_No':1,'P':10000 , 'N': 1, 'R': 0.5, 'S_I':50},         # R = Rate of Interest
{'S_No':2,'P':12000 , 'N': 2, 'R': 1 , 'S_I':240},         # S_I = Simple Interest((P*N*R)/100)
{'S_No':3,'P':14000 , 'N': 2.5, 'R': 1.5, 'S_I':525},
{'S_No':4,'P':16000 , 'N': 3, 'R': 2, 'S_I':960},
{'S_No':5,'P':18000 , 'N': 4, 'R': 2.5, 'S_I':1800},    
]
        # For importing the dictionary data to form CSV file

with open('data.csv', 'w') as csvfile:
    writer = csv.DictWriter(csvfile, fieldnames = Interest_info)
    writer.writeheader()
    writer.writerows(n_dict)
  • Code Output:
 S_No         P          N        R       S_I
  1         10000       1.0      0.5      50
  2         12000       2.0      1.0      240
  3         14000       2.5      1.5      525
  4         16000       3.0      2.0      960
  5         18000       4.0      2.5      1800

For further reference, use the link below to learn more about the Python CSV module.

Leave a Reply

Your email address will not be published. Required fields are marked *