Python provides various modules and functions to work with spreadsheets like Excel, Google sheets, etc., to manipulate data. CSV files can be elegantly processed or shared by converting their content into a dictionary. For this purpose, the functions like csv.DictReader(), pandas.to_dict(), etc., are used in Python.
This blog discusses the below-listed methods to convert CSV files into the dictionary in Python:
- Method 1: Using csv.DictReader() Function
- Method 2: Using the pandas.to_dict() Function
- Method 3: Using List Comprehension
Method 1: Using csv.DictReader() Function
The “csv.DictReader()” function of the “csv” module is used to load the csv data into the dictionary in Python. The example below will show you how to convert the CSV into a dictionary:
The following “csv” file and its data will be used in the below code:
import csv with open("itslinuxfoss.csv", 'r') as f: for line in csv.DictReader(f): print(line)
In the above code:
- The module named “csv” is imported using the “import” keyword at the start of the program.
- The “open()” function opens the specified “csv” file in the “r” mode.
- The “csv.DictReader()” function accepts the “csv” file data and retrieves a dictionary.
The above output verified that the input “csv” file had been successfully converted into a dictionary using the “csv.DataReader()” function.
Method 2: Using the df.to_dict() function
The panda’s module provides functions such as “pf.read_csv()” to read the csv file and “df.data.to_dict()” function to convert the “csv” file into a dictionary. An example code can be found here:
import pandas as pd df_data = pd.read_csv('itslinuxfoss.csv') data = df_data.to_dict(orient='records') print(data)
In the above code:
- The “pd.read_csv()” function accepts the “csv” file name “itslinuxfoss.csv” and reads the file.
- The “df_data.to_dict()” function is used to convert the input csv file into a dictionary.
The above output shows the “Dictionary” value of the “itslinuxfoss.csv” file.
Method 3: Using List Comprehension
The List Comprehension method is also used to get the dictionary from the given csv file. Let’s understand it by the following examples:
import csv with open('itslinuxfoss.csv', 'r') as data: val = csv.reader(data) hdr = next(val) data1 = [dict(zip(hdr, row)) for row in val] print(data1)
In the above code:
- The “open()” function is used to open the “csv” file in “read” mode.
- The “csv.reader()” function reads the data of the csv file into a list of rows and stored in a variable “val”.
- The header of the “csv” file is stored in a separate variable named “hdr”.
- At last, the “List comprehension” method is used along with the “for” loop to iterate over the rows of the input CSV file. Using the zip() function, a dictionary will be created for each row in the “csv”.
- The “zip()” function accepts two arguments, “hdr” and “row,” and returns an iterator of the tuple.
- The “dict()” function accepts the iterators of the tuple and retrieves the dictionary. The key values of the dictionary are column names, and the dictionary’s values are cell names.
- It is necessary to know the column names of the CSV file before using the “List Comprehension” method.
The csv file has been successfully converted into a Dictionary using the “List Comprehension” method.
To convert the “CSV” into a dictionary, the “csv.DictReader()” function, the “pandas.to_dict()” function, and the “List Comprehension” method are used in Python. The “csv.DictReader()” function of the “CSV” module is used to read the “CSV” file and transform it into the dictionary. The “Pandas” module function “pd.read_csv()” is used to read the CSV file, and “pd.to_dict()” is used to convert the “CSV” into the dictionary. This post gave a thorough guide on converting the csv into a dictionary in Python.