HomepythonReading and Writing CSV Files in Python with the csv Module

Reading and Writing CSV Files in Python with the csv Module

CSV (Comma-Separated Values) files are widely used for storing tabular data. Python’s built-in csv module provides functionality to both read from and write to CSV files in a variety of formats. This article will guide you through the various features and functionalities of the csv module.

Overview of the csv Module

The csv module in Python is designed to handle CSV files efficiently. It provides the following classes for reading and writing CSV files:

  • csv.reader: For reading CSV files.
  • csv.writer: For writing to CSV files.
  • csv.DictReader: For reading CSV files into a dictionary.
  • csv.DictWriter: For writing dictionaries to CSV files.

Let’s dive into each of these classes and understand how to use them.

Reading CSV Files

Using csv.reader

The csv.reader class allows you to read CSV files in a straightforward manner. Here’s an example:

import csv

# Reading a CSV file
with open('example.csv', mode='r', newline='') as file:
    csv_reader = csv.reader(file)
    for row in csv_reader:
        print(row)

In this example:

  • The open function opens the file in read mode ('r').
  • The csv.reader object reads the file.
  • The newline='' argument prevents any newline issues across different operating systems.
  • Each row in the CSV file is iterated over and printed.

Using csv.DictReader

The csv.DictReader class reads each row as a dictionary, where the keys are the column headers. Here’s an example:

import csv

# Reading a CSV file into a dictionary
with open('example.csv', mode='r', newline='') as file:
    csv_dict_reader = csv.DictReader(file)
    for row in csv_dict_reader:
        print(row)

In this example:

  • csv.DictReader reads the CSV file and automatically maps the first row (headers) to dictionary keys.
  • Each row is then printed as a dictionary.

Writing CSV Files

Using csv.writer

The csv.writer class allows you to write data to CSV files. Here’s an example:

import csv

# Writing to a CSV file
data = [
    ['Name', 'Age', 'City'],
    ['Alice', 30, 'New York'],
    ['Bob', 25, 'Los Angeles'],
    ['Charlie', 35, 'Chicago']
]

with open('output.csv', mode='w', newline='') as file:
    csv_writer = csv.writer(file)
    csv_writer.writerows(data)

In this example:

  • The open function opens the file in write mode ('w').
  • The csv.writer object writes the data to the file.
  • writerows writes multiple rows at once.

Using csv.DictWriter

The csv.DictWriter class allows you to write dictionaries to CSV files. Here’s an example:

import csv

# Writing dictionaries to a CSV file
data = [
    {'Name': 'Alice', 'Age': 30, 'City': 'New York'},
    {'Name': 'Bob', 'Age': 25, 'City': 'Los Angeles'},
    {'Name': 'Charlie', 'Age': 35, 'City': 'Chicago'}
]

with open('output.csv', mode='w', newline='') as file:
    fieldnames = ['Name', 'Age', 'City']
    csv_dict_writer = csv.DictWriter(file, fieldnames=fieldnames)
    csv_dict_writer.writeheader()
    csv_dict_writer.writerows(data)

In this example:

  • fieldnames defines the order of the columns.
  • writeheader writes the header row to the CSV file.
  • writerows writes multiple dictionaries to the file.

Customizing CSV Reading and Writing

Dialects and Formatting Parameters

The csv module allows customization of CSV reading and writing through dialects and formatting parameters. A dialect is a way to group various formatting parameters under a single name.

Defining a Custom Dialect

import csv

csv.register_dialect('my_dialect', delimiter=';', quotechar='"', quoting=csv.QUOTE_MINIMAL)

# Using the custom dialect
with open('example.csv', mode='r', newline='') as file:
    csv_reader = csv.reader(file, dialect='my_dialect')
    for row in csv_reader:
        print(row)

In this example:

  • register_dialect creates a custom dialect named 'my_dialect'.
  • The custom dialect is then used to read the CSV file.

Formatting Parameters

You can also directly pass formatting parameters to csv.reader and csv.writer without defining a dialect:

import csv

# Customizing CSV reading
with open('example.csv', mode='r', newline='') as file:
    csv_reader = csv.reader(file, delimiter=';', quotechar='"')
    for row in csv_reader:
        print(row)

# Customizing CSV writing
data = [
    ['Name', 'Age', 'City'],
    ['Alice', 30, 'New York'],
    ['Bob', 25, 'Los Angeles'],
    ['Charlie', 35, 'Chicago']
]

with open('output.csv', mode='w', newline='') as file:
    csv_writer = csv.writer(file, delimiter=';', quotechar='"', quoting=csv.QUOTE_MINIMAL)
    csv_writer.writerows(data)

Handling Different Data Types

When dealing with different data types, you may need to perform conversions when reading from or writing to CSV files. Here’s an example of how to handle numerical data:

import csv

# Reading CSV with numerical data
with open('numbers.csv', mode='r', newline='') as file:
    csv_reader = csv.reader(file)
    for row in csv_reader:
        numbers = [int(num) for num in row]
        print(numbers)

# Writing numerical data to CSV
data = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]

with open('numbers.csv', mode='w', newline='') as file:
    csv_writer = csv.writer(file)
    csv_writer.writerows(data)

In this example:

  • Numerical data is converted from strings to integers when reading.
  • Numerical data is directly written as integers.

By understanding and utilizing the various classes and methods provided by the csv module, you can efficiently manage CSV data in your Python applications.

Subscribe
Notify of

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Popular