Skip to Content
Course content

6.2.1 Using the csv module

The csv module in Python provides a straightforward way to work with CSV (Comma Separated Values) files. It includes functionality for reading and writing CSV files, handling delimiters, and managing headers.

Here's a detailed guide on how to use the csv module:

1. Importing the csv Module

Before working with CSV files, you need to import Python’s built-in csv module.

import csv

2. Reading CSV Files

The csv.reader() function reads the content of a CSV file, returning each row as a list of values.

Basic CSV Reading Example:

import csv

# Open a CSV file in read mode
with open('data.csv', mode='r') as file:
    reader = csv.reader(file)  # Create a CSV reader object
    for row in reader:  # Iterate through each row
        print(row)  # Each row is a list of values

Handling CSV Files with Headers Using csv.DictReader():

If the CSV file contains headers, you can use csv.DictReader(), which reads each row as a dictionary, where the header values become the keys.

import csv

# Open a CSV file with headers
with open('data_with_headers.csv', mode='r') as file:
    reader = csv.DictReader(file)  # Read CSV into a dictionary format
    for row in reader:
        print(row)  # Each row is a dictionary with column names as keys

3. Writing to CSV Files

To write data to a CSV file, you use the csv.writer() function. You can write either single rows using writerow() or multiple rows using writerows().

Basic CSV Writing Example:

import csv

# Data to write to CSV
data = [
    ['Name', 'Age', 'City'],
    ['Alice', 30, 'New York'],
    ['Bob', 25, 'San Francisco']
]

# Open the CSV file in write mode
with open('output.csv', mode='w', newline='') as file:
    writer = csv.writer(file)  # Create a CSV writer object
    writer.writerows(data)  # Write multiple rows at once
  • Note: The newline='' argument is important to prevent extra blank lines between rows, especially on Windows.

4. Writing with Headers Using csv.DictWriter()

If you want to write data with headers (like a dictionary format), you can use csv.DictWriter(). This allows you to specify the column names.

import csv

# Data to write
data = [
    {'Name': 'Alice', 'Age': 30, 'City': 'New York'},
    {'Name': 'Bob', 'Age': 25, 'City': 'San Francisco'}
]

# Open the file for writing
with open('output_with_headers.csv', mode='w', newline='') as file:
    fieldnames = ['Name', 'Age', 'City']  # Define column names
    writer = csv.DictWriter(file, fieldnames=fieldnames)
    writer.writeheader()  # Write the header
    writer.writerows(data)  # Write the data rows

5. Custom Delimiters

You can customize the delimiter used to separate the values in the CSV file. For example, if your data uses semicolons (;) instead of commas, you can specify this when reading or writing a file.

Custom Delimiter Example:

import csv

# Open file with a semicolon delimiter
with open('data.csv', mode='r') as file:
    reader = csv.reader(file, delimiter=';')  # Use semicolon as delimiter
    for row in reader:
        print(row)

6. Working with Quote Characters

Sometimes, values in CSV files may contain commas or special characters. You can use quote characters to enclose these values. The default quote character is double quotes ("), but this can be customized.

Example: Handling Quotes in CSV:

import csv

# Open file with custom quote character
with open('data.csv', mode='r') as file:
    reader = csv.reader(file, quotechar='"')  # Specify quote character
    for row in reader:
        print(row)

7. Encoding CSV Files

If you’re working with files encoded in formats other than UTF-8, you can specify the encoding when opening the file. This is particularly useful for files in languages other than English or those with special characters.

Example: Reading a CSV File with Specific Encoding:

import csv

# Open the CSV file with latin1 encoding
with open('data.csv', mode='r', encoding='latin1') as file:
    reader = csv.reader(file)
    for row in reader:
        print(row)

8. Example Use Case: Reading and Writing Data

Here’s a full example of reading data from one CSV file and writing it to another, while modifying the data:

import csv

# Read data from a CSV file
with open('input.csv', mode='r') as file:
    reader = csv.reader(file)
    rows = [row for row in reader]  # Read all rows into a list

# Modify data
for row in rows:
    row[1] = int(row[1]) + 1  # Add 1 to the second column (assumed to be a number)

# Write modified data to a new CSV file
with open('output.csv', mode='w', newline='') as file:
    writer = csv.writer(file)
    writer.writerows(rows)  # Write modified rows

9. Summary

  • csv.reader(): Reads CSV data into a list format.
  • csv.writer(): Writes data into a CSV file in list format.
  • csv.DictReader(): Reads CSV data into dictionaries using the header as keys.
  • csv.DictWriter(): Writes data as dictionaries with specified field names.
  • delimiter: Allows customization of the delimiter used (e.g., semicolons instead of commas).
  • quotechar: Defines the character used to quote fields with special characters.
  • encoding: Specifies file encoding for reading/writing files with non-UTF-8 characters.

The csv module makes it simple to read from and write to CSV files in Python, which is a common requirement when handling data from external sources or for generating reports.

Commenting is not enabled on this course.