Back to Python
2025-12-176 min read

Example 1: Read CSV files with csv.reader() (Python Programming)

Learn Example 1: Read CSV files with csv.reader() (Python Programming) step by step with clear examples and exercises.

Title: Reading CSV Files with csv.reader() (Python Programming)

Why This Matters

In this tutorial, we will delve into reading CSV files using Python's built-in csv module and the csv.reader() function. This skill is indispensable for handling data from various sources in a structured format, which is crucial for data analysis, machine learning, web scraping tasks, and many more applications.

Prerequisites

To follow this tutorial, you should have a foundational understanding of Python programming, including variables, functions, and control structures such as loops and conditionals. Familiarity with CSV files and their structure will also be helpful but is not required as we'll cover the basics in this lesson.

Before diving into the core concept, let's briefly review some key terms related to CSV files:

  • Field: A single piece of data within a record (row) separated by a delimiter.
  • Record: A collection of fields enclosed within quotes or delimited by a separator, typically a comma.
  • Header row: The first row in the CSV file that contains column names.

Core Concept

The csv module is a built-in Python library that allows you to read and write CSV files easily. The csv.reader() function is used to create an iterator that reads rows from a CSV file as lists. Each row corresponds to one line in the file, and each element in the list represents a field or column within that row.

Here's a simple example of how to use csv.reader():

import csv

Open the CSV file

with open('example.csv', 'r') as f:

Create a reader object

reader = csv.reader(f)

Iterate through each row in the CSV file

for row in reader:

print(row)


In this example, we first import the `csv` module and open our sample CSV file named 'example.csv'. We then create a `reader` object by calling `csv.reader()` on the opened file. Finally, we iterate through each row in the CSV file using a for loop, printing out each row as a list.

### Handling Header Rows

When working with CSV files that have header rows, it's essential to skip the first row before processing the data. This can be achieved by calling the `next()` function on the reader object:

Skip the header row (first row with column names)

next(reader)

for row in reader:

print(row)


### Handling Empty Fields or Rows

In some cases, you may encounter CSV files with empty fields or rows. To handle these situations, you can use list comprehension to convert each row to a list and check if it has the expected number of elements before processing:

Correct: Handle empty fields and rows

for row in reader:

if len(row) > 0: # Check if the list is not empty

name, age, city = row

...


### Handling CSV Files with Quotes or Escaped Commas

If your CSV file contains quotes or escaped commas, you may need to use a different constructor for the `csv.reader()` function to handle these cases correctly:

Correct: Handle CSV files with quotes and escaped commas

with open('example.csv', 'r') as f:

reader = csv.reader(f, delimiter=',', quotechar='"')

...

Worked Example

Let's work through an example where we read data from a CSV file and perform some basic operations on it. Suppose our CSV file 'example.csv' contains the following data:

Name,Age,City
Alice,25,New York
Bob,30,Los Angeles
Charlie,28,Chicago

Here's a Python script that reads this CSV file and performs some operations on the data:

import csv

Open the CSV file

with open('example.csv', 'r') as f:

Create a reader object

reader = csv.reader(f)

Skip the header row (first row with column names)

next(reader)

Iterate through each row in the CSV file

total_age = 0

for row in reader:

if len(row) > 0: # Check if the list is not empty

name, age, city = row

print(f"Name: {name}, Age: {age}, City: {city}")

total_age += int(age)

Calculate and print the average age

avg_age = total_age / len(reader)

print(f"Average Age: {avg_age}")


In this example, we first open our CSV file and create a `reader` object. We then skip the header row using the `next()` function and iterate through each row in the CSV file. For each row, we extract the name, age, and city values, print them out, and add the age to our total. After processing all rows, we calculate and print the average age.

Common Mistakes

  1. Not skipping the header row: If your CSV file contains a header row (with column names), make sure to skip it before iterating through the data.
  2. Incorrectly handling empty fields or rows: If your CSV file contains empty fields or rows, you may encounter errors when iterating through the data. To handle these cases, you can use list comprehension to convert each row to a list and check if it has the expected number of elements before processing.
  3. Not handling CSV files with quotes or escaped commas: If your CSV file contains quotes or escaped commas, you may need to use a different constructor for the csv.reader() function to handle these cases correctly.
  4. Assuming all rows have the same number of fields: Make sure to check the number of elements in each row before processing to account for varying numbers of fields per row.
  5. Not properly closing the CSV file: Remember to close the opened file when you're done reading or writing data to it using the close() method on the file object.

Practice Questions

  1. Write a Python script that reads data from a CSV file and calculates the total sum of ages for all rows.
  2. Given a CSV file with columns Name, Age, and Salary, write a Python script that calculates the average salary for each city.
  3. Modify the example script to handle CSV files with quotes and escaped commas.
  4. Write a Python script that reads data from a CSV file and sorts the rows by age in descending order.
  5. Given a CSV file where some fields may contain newline characters, write a Python script that properly handles these cases when reading the data.

FAQ

  1. What if my CSV file has a different delimiter character instead of a comma?

You can specify the delimiter when creating the csv.reader() object: csv.reader(f, delimiter='\t') for tab-separated values (TSV).

  1. How do I write data to a CSV file using Python's csv module?

To write data to a CSV file, you can use the csv.writer() function instead of csv.reader(). Here's an example:

import csv

data = [['Name1', 'Age1', 'City1'], ['Name2', 'Age2', 'City2']]

with open('output.csv', 'w', newline='') as f:
writer = csv.writer(f)
writer.writerows(data)

This script writes the data list to a CSV file named 'output.csv'.

  1. What if my CSV file contains newline characters within fields?

To handle newline characters within fields, you can use the linestrip() method on each row before processing:

Correct: Handle newline characters within fields

for row in reader:

row = row[0].strip('\n') # Remove newline character from first field (assuming it contains newlines)

if len(row) > 0: # Check if the list is not empty

name, age, city = row.split(',')

...

Example 1: Read CSV files with csv.reader() (Python Programming) | Python | XQA Learn