Articles

Python CSV Tutorial: Read, Write, and Edit CSV Files

Published Jul 16, 2024Updated Apr 25, 2025
Learn how to work with CSV files in Python using the built-in `csv` module and `pandas`. This beginner-friendly guide covers reading, writing, and analyzing CSV data with examples and best practices.

What are CSV files?

CSV, Comma Separated Values, is a plain text file format used to store structured tabular data. In a CSV file, a line represents a row in a table. Each line consists of comma-separated values, representing a column in a row. A CSV file is saved with a .csv extension.

In this tutorial, we’ll cover how to read, write, and manipulate CSV files using Python.

Related Course

Introduction to Claude Analysis

Utilize Claude for data insights by managing CSV files, handling data, performing statistical analysis, using natural language queries, and creating visualizations.Try it for free

What are CSV files used for?

CSV files are extensively used across various applications and platforms. Some of the advantages of CSV files are listed below:

  • Data Analysis: CSV files are used to analyze data and to manipulate datasets for analysis.

  • Spreadsheet Applications: CSV files allow easy data manipulation and sharing using various spreadsheet applications, such as Microsoft Excel and Google Sheets.

  • Customer Relationship Management (CRM): CSV files are used to manage and analyze data, such as managing contact lists, campaign data, and customer management.

  • E-commerce and Inventory Management: CSV files help manage product listings and update inventory to allow data to be coordinated in bulk.

Imagine you are a Data Analyst at a startup. You regularly receive data from many sources. To manage the data, you start by storing it in a CSV file and processing it using a programming language such as Python.

Setting up the Python CSV environment

Before you start working with CSV files in Python, setting up your environment properly is crucial.

If you don’t have Python installed on your system, visit this link to install it. To work with CSV files in Python, we will use the CSV module part of the standard library (no need to install it). We import the CSV module like so:

import csv

Now that we have the environment ready to use the CSV module, let’s learn what operations we can perform.

How to read and write CSV files in Python

We can perform operations like adding a new column or filtering out the data using a Python program.

First, let’s learn the basic operations like opening, reading, and writing CSV files.

Opening a CSV file using Python’s open() function

Let’s see how to open a CSV file using Python. A CSV file can be opened using the built-in function open() with the appropriate mode ('r' for reading, 'w' for writing, or 'a' for appending). It’s better to use the with statement, which automatically handles the closing of the file even if an error occurs.

Let’s look at an example to understand how to open CSV files with Python. In a CSV file named example.csv, we have the following data:

Name,Age,Department  
Alice,30,HR  
Bob,24,IT  
Charlie,28,Finance  

The following code opens the example.csv file using the open() function and with read mode.

Then we use the next() function to skip the header row and a for loop to iterate over each row in the CSV file:

import csv
# Open the CSV file in read mode
with open('example.csv', mode='r') as file:
# Create a CSV reader object
csv_reader = csv.reader(file)
# Skip the header row (if there is one)
next(csv_reader, None)
# Iterate over each row in the CSV file
for row in csv_reader:
print(row)

Now that we know how to open a CSV file, let’s dive into how we can read its contents using Python’s csv module.

Reading CSV files with Python’s csv module

To see how we can read CSV files with Python, let’s look at an example. In a CSV file named example.csv, we have the following information:

Name,Age,Department 
Alice,30,HR 
Bob,24,IT 
Charlie,28,Finance 

We write a Python script to read this CSV file and print its contents like so:

import csv
# Open the CSV file
with open('example.csv', mode= 'r') as file:
# Create a CSV reader object
csv_reader = csv.reader(file)
# Read the header
header = next(csv_reader)
print(f"Header: {header}")
# Read each row of the CSV file
for row in csv_reader:
print(f"Row: {row}")

In this code, we create a CSV reader object using csv.reader(file), which reads a CSV file and returns each row as a list of strings.

Next, we read the header of the CSV file using the next(csv_reader) function which is used to retrieve the next item from an iterator. A header in a CSV file is the first row that contains the names of the columns, providing a label for each column’s data.

Then, we loop through the remaining rows in the CSV file, printing each row as a list of strings. This approach allows us to read and process CSV data in Python easily.

Once we’re comfortable reading CSV data, the next step is to learn how to write data into CSV files using Python.

Writing to a CSV file using the writer object

To write the data into a CSV file, we first open the file in write mode using the with statement and then create a writer object using the csv.writer(file) to allow us to write the data into the file.

To understand it more concisely, let’s look at an example. In a CSV file named output.csv, we have the following information:

Name,Age,Department 
Alice,30,HR 
Bob,24,IT 
Charlie,28,Finance 

Here’s the code snippet that opens the output.csv file and defines a list containing all the data that is to be written into the CSV file.

Then, the csv.writer(file) object writes the data into the output.csv file by passing the list containing the header row and all subsequent data rows as arguments to the writerows() function:

import csv
# Data to be written to CSV
data = [
["Name", "Age", "Department"],
["Alice", 30, "HR"],
["Bob", 24, "IT"],
["Charlie", 28, "Finance"]
]
# Open the CSV file in write mode
with open('output.csv', mode='w', newline='') as file:
# Create a CSV writer object
csv_writer = csv.writer(file)
# Write the rows to the CSV file
csv_writer.writerows(data)

After reading and writing CSV files, let’s explore how to manipulate and analyze CSV data to extract meaningful insights. 

### How to analyze and modify CSV files in Python

Data manipulation and analysis are essential skills for any data analyst or scientist. In this section, we will look at basic data manipulation in Python.  

The following are some data analysis things we can do with the CSV module in Python 

#### Filtering rows in CSV data using Python

Filtering data involves selecting rows that meet certain criteria. For example, we might want to filter rows where the employees' ages are greater than 25. 

Here’s how we can filter the data for that: 

```py
filtered_data = [row for row in data if int(row['Age']) > 25] 

# Display the filtered data 
for row in filtered_data: 
    print(row) 

Adding new columns to CSV data in Python

Adding a new column involves creating additional data based on the existing columns or with new data. For instance, we might want to add a column that calculates each employee’s age next year.

Here’s how you can add a new column:

# Add a new column 'Age Next Year'
for row in data:
row['age_next_year'] = int(row['Age']) + 1
# Display the data with the new column
for row in data:
print(row)

This loop iterates over each row in the data list. Each row adds a new key-value pair where the key is ‘age_next_year’ and the value is the current age incremented by one. This creates a new column in the dataset.

We’ve successfully used the csv module to perform some basic operations on the files.

Conclusion

In this tutorial, we explored CSV files and the csv module. Let’s recap what we’ve discussed:

  • CSV files provide a standardized format for storing tabular data, ensuring compatibility across various platforms and applications.
  • Python’s csv module offers efficient tools for reading, writing, and manipulating CSV files, simplifying data management tasks.
  • Data manipulation operations, such as filtering rows and adding new columns, can be easily performed using Python’s csv module.
  • Python’s simplicity and versatility make it a powerful tool for handling CSV files, facilitating seamless integration into data analysis workflows.

These were some basics of working with CSV files using Python. For more advanced data analysis tasks, libraries like Pandas can be integrated with Python to enhance CSV file processing capabilities.

To build your Python skills and learn more about working with files, check out our Learn Python 3 course.

Frequently asked questions

1. How do I handle a large CSV file in Python?

To handle large CSV files, use efficient techniques like reading the file line by line with Python’s built-in csv module or using the pandas library with chunking (pd.read_csv(..., chunksize=...)). These methods help manage memory usage and avoid performance issues.

2. Why is CSV used in Python?

CSV files are easy to read and write, making them ideal for storing and exchanging structured data. Python supports CSV handling through built-in tools, allowing developers to work with tabular data quickly and efficiently.

3. How do I edit a CSV file in Python?

To edit a CSV file, read its content into memory using the csv.reader() or pandas.read_csv(), modify the data as needed, and then write it back using csv.writer() or pandas.to_csv().

4. Is csv a standard Python library?

Yes, the csv module is part of Python’s standard library. You can use it without any external installation by importing it with import csv.

Codecademy Team

'The Codecademy Team, composed of experienced educators and tech experts, is dedicated to making tech skills accessible to all. We empower learners worldwide with expert-reviewed content that develops and enhances the technical skills needed to advance and succeed in their careers.'

Meet the full team