How to save a Python Dictionary to a CSV File? - GeeksforGeeks

01 Jul, 2020
Prerequisites: Working with csv files in Python
CSV (comma-separated values) files are one of the easiest ways to transfer data in form of string especially to any spreadsheet program like Microsoft Excel or Google spreadsheet. In this article, we will see how to save a PYthon dictionary to a CSV file. Follow the below steps for the same.
Import csv moduleimport csvCreating list of field namesfield_names= ['No', 'Company', 'Car Model']Creating a list of python dictionariescars = [{‘No’: 1, ‘Company’: ‘Ferrari’, ‘Car Model’: ‘488 GTB’},{‘No’: 2, ‘Company’: ‘Porsche’, ‘Car Model’: ‘918 Spyder’},{‘No’: 3, ‘Company’: ‘Bugatti’, ‘Car Model’: ‘La Voiture Noire’},{‘No’: 4, ‘Company’: ‘Rolls Royce’, ‘Car Model’: ‘Phantom’},{‘No’: 5, ‘Company’: ‘BMW’, ‘Car Model’: ‘BMW X7’},]Writing content of dictionaries to CSV filewith open('Names.csv', 'w') as csvfile:
    writer = csv.DictWriter(csvfile, fieldnames=field_names)
    writer.writeheader()
    writer.writerows(cars)Syntax:DictWriter( (filename), fieldnames = [list of field names] )In the above code snippet writer is an instance of csv.DictWriter class and uses two of its following methods:DictWriter.writeheader() is used to write a row of column headings / field names to the given CSV filecsvwriter.writerows() method is used to write rows of data into the specified file.Note: To write a single dictionary in CSV file use writerow() method
Import csv moduleimport csv
import csv
Creating list of field namesfield_names= ['No', 'Company', 'Car Model']
field_names= ['No', 'Company', 'Car Model']
Creating a list of python dictionariescars = [{‘No’: 1, ‘Company’: ‘Ferrari’, ‘Car Model’: ‘488 GTB’},{‘No’: 2, ‘Company’: ‘Porsche’, ‘Car Model’: ‘918 Spyder’},{‘No’: 3, ‘Company’: ‘Bugatti’, ‘Car Model’: ‘La Voiture Noire’},{‘No’: 4, ‘Company’: ‘Rolls Royce’, ‘Car Model’: ‘Phantom’},{‘No’: 5, ‘Company’: ‘BMW’, ‘Car Model’: ‘BMW X7’},]
cars = [{‘No’: 1, ‘Company’: ‘Ferrari’, ‘Car Model’: ‘488 GTB’},{‘No’: 2, ‘Company’: ‘Porsche’, ‘Car Model’: ‘918 Spyder’},{‘No’: 3, ‘Company’: ‘Bugatti’, ‘Car Model’: ‘La Voiture Noire’},{‘No’: 4, ‘Company’: ‘Rolls Royce’, ‘Car Model’: ‘Phantom’},{‘No’: 5, ‘Company’: ‘BMW’, ‘Car Model’: ‘BMW X7’},]
Writing content of dictionaries to CSV filewith open('Names.csv', 'w') as csvfile:
    writer = csv.DictWriter(csvfile, fieldnames=field_names)
    writer.writeheader()
    writer.writerows(cars)Syntax:DictWriter( (filename), fieldnames = [list of field names] )In the above code snippet writer is an instance of csv.DictWriter class and uses two of its following methods:DictWriter.writeheader() is used to write a row of column headings / field names to the given CSV filecsvwriter.writerows() method is used to write rows of data into the specified file.Note: To write a single dictionary in CSV file use writerow() method
with open('Names.csv', 'w') as csvfile:
    writer = csv.DictWriter(csvfile, fieldnames=field_names)
    writer.writeheader()
    writer.writerows(cars)
Syntax:
DictWriter( (filename), fieldnames = [list of field names] )
In the above code snippet writer is an instance of csv.DictWriter class and uses two of its following methods:
DictWriter.writeheader() is used to write a row of column headings / field names to the given CSV filecsvwriter.writerows() method is used to write rows of data into the specified file.
DictWriter.writeheader() is used to write a row of column headings / field names to the given CSV file
csvwriter.writerows() method is used to write rows of data into the specified file.
Note: To write a single dictionary in CSV file use writerow() method
Complete code to write python dictionaries in CSV file
import csv  field_names = ['No', 'Company', 'Car Model']  cars = [{'No': 1, 'Company': 'Ferrari', 'Car Model': '488 GTB'},{'No': 2, 'Company': 'Porsche', 'Car Model': '918 Spyder'},{'No': 3, 'Company': 'Bugatti', 'Car Model': 'La Voiture Noire'},{'No': 4, 'Company': 'Rolls Royce', 'Car Model': 'Phantom'},{'No': 5, 'Company': 'BMW', 'Car Model': 'BMW X7'},]  with open('Names.csv', 'w') as csvfile:    writer = csv.DictWriter(csvfile, fieldnames = field_names)    writer.writeheader()    writer.writerows(cars)
Output:
python-dict
Python
python-dict
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Read a file line by line in Python
Enumerate() in Python
How to Install PIP on Windows ?
Iterate over a list in Python
Different ways to create Pandas Dataframe
Python program to convert a list to string
Reading and Writing to text files in Python
*args and **kwargs in Python
How To Convert Python Dictionary To JSON?