Working with data is an inevitable part of programming, and as someone who often finds themselves knee-deep in various file formats, I’ve always appreciated how Python simplifies the whole process.
One such file format that comes up regularly, particularly in data analysis, is the CSV file.
The CSV, or Comma-Separated Values, is a popular data exchange format due to its simplicity.
Luckily, Python comes with a built-in module called csv
, which makes working with these files remarkably efficient.
In this article, I’ll break down how the csv
module works in Python, from basic usage to more advanced techniques that can save you tons of time when processing data.
What Is a CSV File?
Before diving into the csv
module, let’s start with a basic understanding of what a CSV file is.
A CSV file is essentially a plain text file where each line represents a row of data, and each value is separated by a comma (or sometimes other delimiters like tabs).
Here's a quick example of what it might look like:
Name,Age,Occupation
Alice,30,Engineer
Bob,25,Data Scientist
Charlie,35,Teacher
Why the csv
Module?
You might wonder why you'd need the csv
module when CSV files are just text files that could theoretically be read using Python's standard file handling methods.
While this is true, CSV files can have complexities—like embedded commas, line breaks within cells, and different delimiters—that are tricky to handle manually.
The csv
module abstracts all of this, letting you focus on your data.
Get the eBook
Inside, you'll discover a plethora of Python secrets that will guide you through a journey of learning how to write cleaner, faster, and more Pythonic code. Whether it's mastering data structures, understanding the nuances of object-oriented programming, or uncovering Python's hidden features, this ebook has something for everyone.
Reading CSV Files
Let’s jump into the code.
The most common operation you'll perform on a CSV file is reading its contents.
The csv.reader()
function in the module is an easy-to-use tool for that.
Here's a step-by-step guide on how to do it.
Basic CSV Reading
import csv
# Open a CSV file
with open('example.csv', 'r') as file:
reader = csv.reader(file)
# Iterate over the rows
for row in reader:
print(row)
This is the simplest way to read a CSV file.
The csv.reader()
returns an iterable, where each iteration gives you a list representing a row of the file.
Handling Headers
Most CSV files come with headers in the first row, like column names.
If you don’t need these headers, you can simply skip the first row when iterating:
import csv
with open('example.csv', 'r') as file:
reader = csv.reader(file)
# Skip header
next(reader)
for row in reader:
print(row)
Sometimes, I’m working with files that contain a mix of useful and irrelevant data, and I find myself skipping rows based on more than just the header.
You can do this easily within the for
loop.
DictReader: A More Intuitive Way to Read CSV Files
If your CSV file has headers, the csv.DictReader()
is another fantastic option that reads each row as a dictionary, with the keys being the column names:
import csv
with open('example.csv', 'r') as file:
reader = csv.DictReader(file)
for row in reader:
print(row)
This approach can make your code more readable and intuitive, especially when working with large datasets.
For example, accessing row['Name']
feels much clearer than dealing with index-based access like row[0]
.
Writing to CSV Files
Once you’ve read and processed your data, chances are you'll want to save or export it.
The csv.writer()
function is your go-to tool for writing to CSV files.
Basic CSV Writing
import csv
# Data to be written
data = [
['Name', 'Age', 'Occupation'],
['Alice', 30, 'Engineer'],
['Bob', 25, 'Data Scientist'],
['Charlie', 35, 'Teacher']
]
# Open a file in write mode
with open('output.csv', 'w', newline='') as file:
writer = csv.writer(file)
# Write data to the file
writer.writerows(data)
The writer.writerows()
function takes a list of lists and writes them to the CSV file, where each inner list represents a row of data.
DictWriter: A Cleaner Way to Write CSV Files
Just as we have DictReader
for reading CSV files into dictionaries, we have DictWriter
for writing dictionaries to a CSV.
This method can be particularly handy when you want to specify your column names explicitly.
This article is for paid members only
To continue reading this article, upgrade your account to get full access.
Subscribe NowAlready have an account? Sign In