Skip to content

How to Effortlessly Use csv DictReader in Python?

[

Reading CSVs With Python’s “csv” Module

In this tutorial, we will learn how to read CSV files in Python using the “csv” module. CSV (Comma Separated Values) files are commonly used for storing and exchanging tabular data. The “csv” module in Python provides functionality for working with CSV files, allowing us to read and write data in this format.

Why Use the “csv” Module?

Using the “csv” module provides several advantages over manually parsing CSV files. It handles various edge cases, such as values containing commas or quotes, and supports different delimiters. Additionally, it simplifies the process of reading and accessing data from CSV files, especially when working with large datasets.

Reading CSV Files with csv.Reader()

The first method of reading CSV files using the “csv” module is by using the csv.Reader() class. This method allows you to access CSV data using indexes and is ideal for simple CSV files.

Here is an example of how to read a CSV file using csv.Reader():

import csv
with open('employee_birthday.csv') as csv_file:
csv_reader = csv.Reader(csv_file, delimiter=',')
line_count = 0
for row in csv_reader:
print(f'\t{row[0]} works in the {row[1]} department, and was born in {row[2]}')

In this example, we open the CSV file using open() and specify the delimiter as a comma. We then create a csv_reader object using csv.Reader(), passing in the CSV file object and the delimiter. We initialize a line_count variable to keep track of the line number.

Next, we iterate over each row in the CSV file using a for loop, and print the desired data from each row. The index is used to access the data in the row. Finally, we increase the line_count by 1 to move to the next row.

Reading CSV Files with csv.DictReader()

The second method of reading CSV files is by using the csv.DictReader() class. This method provides a friendlier interface and is easier to use, especially when working with large CSV files or when the CSV file contains column headers.

Here is an example of how to read a CSV file using csv.DictReader():

import csv
with open('employee_birthday.csv') as csv_file:
csv_reader = csv.DictReader(csv_file, delimiter=',')
for row in csv_reader:
print(f'\t{row["name"]} works in the {row["department"]} department, and was born in {row["month"]}')

In this example, we follow the same process of opening the CSV file and creating a csv_reader object. However, instead of accessing data using indexes, we access the data using the header names as keys in the row dictionary.

This approach simplifies the code and makes it more readable, as we can clearly identify the data we are accessing by using the header names.

Summary

In this tutorial, we learned how to read CSV files in Python using the “csv” module. We explored two methods: csv.Reader() and csv.DictReader(). The csv.Reader() method allows us to access data using indexes and is suitable for simple CSV files, while csv.DictReader() provides a friendlier interface and is easier to use, especially for large CSV files or files with column headers.

By utilizing the “csv” module in Python, we can efficiently read and access CSV data, making it easier to manipulate and analyze tabular data.