CSV (Comma Separated Values) files are widely used for storing tabular data in a simple, text-based format. They are easy to read and edit with various applications, including spreadsheet software like Microsoft Excel and Google Sheets. In Python, working with CSV files is very straightforward thanks to the built-in csv module. In this lesson, we’ll cover:
- Reading CSV Files: How to read data from CSV files.
- Writing to CSV Files: How to write data to CSV files.
- Handling Headers and Rows: Managing headers and row data in CSV files.
- Using the csvModule: The tools and functions provided by Python’s csv
1. Reading CSV Files
When you need to read data from a CSV file in Python, the csv.reader() function is used. This function allows you to read each row of data and convert it into a Python list or dictionary.
Example: Reading CSV Data
Let’s start with a simple example. Suppose you have a CSV file named students.csv that contains the following data:
Alice,23,A
Bob,25,B
Charlie,22,A
To read this CSV file and access the data in Python, you can use the following code:
# Open the CSV file
with open(‘students.csv’, ‘r’) as file:
# Create a CSV reader object
csv_reader = csv.reader(file)
# Loop through each row in the CSV file
for row in csv_reader:
print(row)
Output:
css
CopyEdit
[‘name’, ‘age’, ‘grade’]
[‘Alice’, ’23’, ‘A’]
[‘Bob’, ’25’, ‘B’]
[‘Charlie’, ’22’, ‘A’]
In this example:
- reader(file)reads the CSV file and returns each row as a list of strings.
- The forloop iterates over each row, printing it.
2. Writing to CSV Files
Writing data to a CSV file is just as simple. You can use the csv.writer() function to write rows of data to a file. Each row is typically a list of values.
Example: Writing to CSV
Suppose you want to write the following data into a CSV file:
[‘name’, ‘age’, ‘grade’],
[‘Alice’, 23, ‘A’],
[‘Bob’, 25, ‘B’],
[‘Charlie’, 22, ‘A’]
]
# Open a file in write mode
with open(‘output.csv’, ‘w’, newline=”) as file:
# Create a CSV writer object
csv_writer = csv.writer(file)
# Write all rows to the CSV file
csv_writer.writerows(data)
In this example:
- The writer(file)creates a writer object that will write to output.csv.
- writerows(data)writes all the rows (each row is a list) into the CSV file.
The newline=” argument ensures that no extra blank lines are added when writing to the CSV file (this is platform-dependent behavior).
3. Handling Headers and Rows
In CSV files, headers are the first row, often used to define the column names, while subsequent rows contain the data. When working with CSV files, it is common to handle headers and rows separately, especially when processing data.
Reading with Headers
If the CSV file contains headers, you can use csv.DictReader() instead of csv.reader(). This allows each row to be accessed as a dictionary, where the keys are the column names (headers) and the values are the corresponding data.
Example: Reading CSV with Headers
Given a CSV file students.csv like this:
Alice,23,A
Bob,25,B
Charlie,22,A
You can read it and access each row as a dictionary:
# Open the CSV file
with open(‘students.csv’, ‘r’) as file:
# Create a CSV dictionary reader object
csv_reader = csv.DictReader(file)
# Loop through each row
for row in csv_reader:
print(row)
Output:
{‘name’: ‘Alice’, ‘age’: ’23’, ‘grade’: ‘A’}
{‘name’: ‘Bob’, ‘age’: ’25’, ‘grade’: ‘B’}
{‘name’: ‘Charlie’, ‘age’: ’22’, ‘grade’: ‘A’}
In this example:
- DictReader(file)reads the CSV file into a dictionary, where the header names (e.g., name, age, grade) become the keys.
- Each row is represented as a dictionary, which makes it easier to access specific columns by their header names.
Writing with Headers
If you want to include headers when writing data to a CSV file, you can use csv.DictWriter() to write the data as a dictionary.
Example: Writing CSV with Headers
# Data to be written
data = [
{‘name’: ‘Alice’, ‘age’: 23, ‘grade’: ‘A’},
{‘name’: ‘Bob’, ‘age’: 25, ‘grade’: ‘B’},
{‘name’: ‘Charlie’, ‘age’: 22, ‘grade’: ‘A’}
]
# Open the file in write mode
with open(‘output_with_headers.csv’, ‘w’, newline=”) as file:
# Define the column headers
fieldnames = [‘name’, ‘age’, ‘grade’]
# Create a CSV writer object
csv_writer = csv.DictWriter(file, fieldnames=fieldnames)
# Write the header row
csv_writer.writeheader()
# Write all rows
csv_writer.writerows(data)
In this example:
- DictWriter(file, fieldnames=fieldnames)creates a writer object that writes dictionaries to the CSV file.
- writeheader()writes the headers first.
- writerows(data)writes the dictionary rows.
4. Using the csv Module
Python’s csv module provides several helpful functions to work with CSV files. Below is an overview of some useful functions in the csv module:
Common Functions in csv Module:
- reader(file): Reads a CSV file and returns an iterator that yields each row as a list.
- writer(file): Creates a writer object that writes rows to a CSV file.
- DictReader(file): Reads a CSV file and returns each row as a dictionary, with the header as the keys.
- DictWriter(file, fieldnames): Writes data to a CSV file as dictionaries, with specified fieldnames as headers.
- field_size_limit(): Returns or sets the maximum field size allowed in a CSV file.
- QUOTE_MINIMAL, csv.QUOTE_ALL, csv.QUOTE_NONNUMERIC, csv.QUOTE_NONE: Constants for controlling how quotes are handled in CSV files.
5. Conclusion
In this lesson, we learned how to work with CSV files using Python’s csv module:
- Reading CSV files: We used reader()and csv.DictReader() to read and process data from CSV files.
- Writing to CSV files: We used writer()and csv.DictWriter() to write data to CSV files, with and without headers.
- Handling headers and rows: We explored how to handle CSV headers and rows efficiently, making it easy to manipulate data.
- Using the csvmodule: We covered various functions in the csv module that simplify working with CSV files in Python.
CSV files are an essential part of data exchange in many domains, and Python provides a clean and simple way to read, write, and process them. With these tools, you can easily handle data stored in CSV format, whether it’s from files or external sources like APIs.
Leave a Reply