csv
The Python csv module provides functionality to read from and write to CSV (comma-separated values) files, which are a common format for data interchange.
The module offers both high-level and low-level interfaces for handling CSV data.
Here’s a quick example:
>>> import csv
>>> with open("people.csv", newline="", encoding="utf-8") as csv_file:
... for row in csv.reader(csv_file):
... print(row)
...
['Name', 'Age', 'Occupation']
['Alice', '30', 'Engineer']
['Bob', '25', 'Data Scientist']
This code will work with the following people.csv file:
people.csv
Name,Age,Occupation
Alice,30,Engineer
Bob,25,Data Scientist
Key Features
- Reads CSV files into lists or dictionaries
- Writes data to CSV files from lists or dictionaries
- Supports custom delimiters and quoting characters
- Handles CSV dialects for different CSV formats
Frequently Used Classes and Functions
| Object | Type | Description |
|---|---|---|
csv.reader |
function | Reads CSV data from a file or iterable |
csv.writer |
function | Writes CSV data to a file |
csv.DictReader |
class | Reads CSV data into a dictionary |
csv.DictWriter |
class | Writes dictionary data to CSV format |
csv.register_dialect |
function | Registers a new dialect with the CSV module |
csv.get_dialect |
function | Returns a dialect object |
Examples
Writing data to a CSV file:
>>> data = [
... {'Name': 'Alice', 'Age': 30, 'Occupation': 'Engineer'},
... {'Name': 'Bob', 'Age': 25, 'Occupation': 'Data Scientist'}
... ]
>>> with open("people.csv", "w", newline="", encoding="utf-8") as csv_file:
... fieldnames = ["Name", "Age", "Occupation"]
... writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
... writer.writeheader()
... for row in data:
... writer.writerow(row)
...
Common Use Cases
- Reading data from CSV files for analysis
- Writing data to CSV files for sharing or storage
- Converting between different CSV dialects
Real-World Example
You have a CSV file with employee data. You want to:
- Read the data.
- Filter out employees under age 30.
- Write the filtered list to a new CSV file.
Here’s a sample CSV file:
employees.csv
id,name,age,department,salary
101,Alice,28,Engineering,70000
102,Bob,35,Marketing,65000
103,Charlie,22,Sales,40000
104,Diana,45,HR,80000
105,Edward,29,Engineering,72000
Below is the Python code to perform the needed operations:
>>> import csv
>>> employees = []
>>> with open("employees.csv", newline="", encoding="utf-8") as infile:
... reader = csv.DictReader(infile)
... for row in reader:
... row["age"] = int(row["age"])
... employees.append(row)
...
>>> filtered = [employee for employee in employees if employee["age"] >= 30]
>>> with open("filtered.csv", "w", newline="", encoding="utf-8") as outfile:
... fieldnames = ["id", "name", "age", "department", "salary"]
... writer = csv.DictWriter(outfile, fieldnames=fieldnames)
... writer.writeheader()
... writer.writerows(filtered)
...
31
The DictReader class uses the headers as keys and generates dictionaries with the values in each row. In the loop, you convert the age field into a number using int() to allow filtering and to store the data into a list.
Then, you use a comprehension to apply the filtering and finally write the resulting data to a new CSV file called filtered.csv using DictWriter.