CSV files are used to store structured data where each row in the csv file stores an entry in which each value is associated with the column name. Similarly, a python dictionary is used to store key-value pairs in Python. In this article, we will discuss how we can read a csv file into a list of dictionaries in python.
Read CSV Into List of Dictionaries Using csv.DictReader()
In python, we can use the csv module to work with csv files. To read a csv file into a list of dictionaries, we will create a csv.DictReader
object using the csv.DictReader()
method. After creating the DictReader
object, we can create a list of dictionaries from the csv file using the following steps.
- First, we will open the csv file using the
open()
function in the read mode. Theopen()
function takes the file name as its first input argument and the literal “r” as its second input argument to show that the file is opened in the read mode. After execution, it returns a file object that contains the csv file. - After obtaining the file object from the
open()
function, we will create aDictReader
object using thecsv.DictReader()
function. Thecsv.DictReader()
function takes the file object as its input argument and returns aDictReader
object. - The
DictReader
object works as an iterator and contains each row of the csv file as a dictionary. In the dictionary, the keys consist of the column names of the csv file whereas the values associated with the keys are values present in a particular column in a row. - To read the csv file into list of dictionaries, we will first create an empty list. After that, we will we will add each dictionary from the
DictReader
object to the list using a for loop. After execution of the for loop, we will get the entire csv file as a list of dictionaries. - Don’t forget to close the file using the
close()
method at the end of the program.
The program to read a csv into list of dictionaries using the DictReader()
method and a for loop is as follows.
import csv
myFile = open('Demo.csv', 'r')
reader = csv.DictReader(myFile)
myList = list()
for dictionary in reader:
myList.append(dictionary)
print("The list of dictionaries is:")
print(myList)
Output:
The list of dictionaries is:
[{'Roll': '1', 'Name': 'Aditya', 'Language': 'Python'}, {'Roll': '2', 'Name': 'Sam', 'Language': ' Java'}, {'Roll': '3', 'Name': ' Chris', 'Language': ' C++'}]
Instead of using the for loop, you can also use the list()
constructor to convert the DictReader
object into a list as follows.
import csv
myFile = open('Demo.csv', 'r')
reader = csv.DictReader(myFile)
myList = list(reader)
print("The list of dictionaries is:")
print(myList)
Output:
The list of dictionaries is:
[{'Roll': '1', 'Name': 'Aditya', 'Language': 'Python'}, {'Roll': '2', 'Name': 'Sam', 'Language': ' Java'}, {'Roll': '3', 'Name': ' Chris', 'Language': ' C++'}]
Conclusion
In this article, we have discussed how we can read a csv file into a list of dictionaries in python. To learn more about lists, you can read this article on list comprehension in python. You might also like this article on dictionary comprehension in python.
The post Read CSV Into List of Dictionaries in Python appeared first on PythonForBeginners.com.