Skip to Content
Course content

6.2 Working with CSV Files

CSV (Comma Separated Values) files are a common format for storing data in tabular form, with each row representing a record and each column representing a field. Python makes it easy to work with CSV files using the built-in csv module.

Here's a detailed explanation of how to read from and write to CSV files, including handling various data types, and the importance of CSV files in data processing.

1. Importing the csv Module

To work with CSV files in Python, you need to import the csv module.

import csv

2. Reading from a CSV File

The csv.reader() function reads a CSV file and returns an iterator that can be used to iterate over rows in the file. Each row is represented as a list of values.

Syntax:

csv.reader(file)

Steps:

  1. Open the CSV file in read mode using open().
  2. Use csv.reader() to read the content.
  3. Iterate over the rows to access the data.

Example: Reading a CSV file:

import csv

# Open the 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 over each row in the CSV
        print(row)  # Print each row (which is a list of values)
  • Output: Each row in the CSV file will be printed as a list of values.

3. Writing to a CSV File

To write data to a CSV file, you use csv.writer(). This function allows you to write rows to a CSV file.

Syntax:

csv.writer(file)

Steps:

  1. Open the file in write ('w') or append ('a') mode using open().
  2. Use csv.writer() to create a writer object.
  3. Use writer.writerow() to write a single row, or writer.writerows() to write multiple rows at once.

Example: Writing to a CSV file:

import csv

# Data to write
data = [
    ["Name", "Age", "City"],
    ["Alice", 30, "New York"],
    ["Bob", 25, "San Francisco"],
    ["Charlie", 35, "Los Angeles"]
]

# Open the file in write mode (creates the file if it doesn't exist)
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 in open() is important to avoid adding extra blank lines between rows when writing to CSV files in Python on some platforms.

4. Handling Headers in CSV Files

When reading or writing CSV files, you may want to include headers (column names). You can easily handle headers using csv.DictReader() and csv.DictWriter(), which read/write rows as dictionaries instead of lists.

Reading with Headers using csv.DictReader():

import csv

# Open the CSV file
with open('data_with_headers.csv', mode='r') as file:
    reader = csv.DictReader(file)  # Read CSV into a dictionary
    for row in reader:
        print(row)  # Each row is now a dictionary with column headers as keys
  • Example Output:
{'Name': 'Alice', 'Age': '30', 'City': 'New York'}
{'Name': 'Bob', 'Age': '25', 'City': 'San Francisco'}
{'Name': 'Charlie', 'Age': '35', 'City': 'Los Angeles'}

Writing with Headers using csv.DictWriter():

import csv

# Data to write
data = [
    {"Name": "Alice", "Age": 30, "City": "New York"},
    {"Name": "Bob", "Age": 25, "City": "San Francisco"},
    {"Name": "Charlie", "Age": 35, "City": "Los Angeles"}
]

# Open the file in write mode
with open('output_with_headers.csv', mode='w', newline='') as file:
    fieldnames = ['Name', 'Age', 'City']  # Define the header
    writer = csv.DictWriter(file, fieldnames=fieldnames)
    writer.writeheader()  # Write the header to the CSV
    writer.writerows(data)  # Write the data rows
  • Note: The writeheader() method writes the header row before writing the data rows.

5. Working with Delimiters and Quote Characters

You can customize the delimiter (the character separating columns) and the quote character (used for fields with special characters, like commas) using parameters like delimiter and quotechar.

Example: Custom Delimiters:

import csv

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

Example: Handling Quote Characters:

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 fields with commas
    for row in reader:
        print(row)

6. Working with CSV Files in Different Encodings

If your CSV files are encoded in formats other than the default UTF-8, you can specify the encoding when opening the file using the encoding parameter.

Example: Reading CSV with a specific encoding:

import csv

# Open file with a different encoding
with open('data.csv', mode='r', encoding='latin1') as file:
    reader = csv.reader(file)
    for row in reader:
        print(row)
  • Common encodings: UTF-8, Latin-1, ISO-8859-1.

Summary

  • csv.reader(): Reads CSV files, with each row represented as a list of values.
  • csv.writer(): Writes data to a CSV file.
  • csv.DictReader(): Reads CSV files into dictionaries (useful for headers).
  • csv.DictWriter(): Writes dictionaries to CSV files with headers.
  • delimiter: Customize the delimiter used in the CSV file.
  • quotechar: Customize the quote character for fields containing commas or special characters.
  • Encoding: Specify the file encoding to handle different formats.

CSV files are widely used for data storage and exchange, and Python provides a powerful, built-in toolset for reading and writing them.

Commenting is not enabled on this course.