csv23: Python 2/3 Unicode CSV Compatibility Layer

0.3.4 · maintenance · verified Thu Apr 16

csv23 provides the unicode-based API of the Python 3 `csv` module for Python 2 and 3. It allows code to run under both versions of Python by abstracting the bytes vs. text differences and adhering to the newer unicode-based interface. The library defaults to UTF-8 encoding and addresses several known bugs in the standard library's `csv` module. The current version is 0.3.4, and it is primarily in maintenance mode for cross-Python 2/3 compatibility features.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `csv23.open_csv` as a context manager for both writing and reading CSV files, ensuring proper encoding handling. It also shows how to use `DictReader` to parse rows as dictionaries.

import csv23
import os

# Create a dummy CSV file for demonstration
file_path = 'example.csv'
data_to_write = [
    ['Name', 'Age', 'City'],
    ['Alice', '30', 'New York'],
    ['Bob', '24', 'London'],
    ['Charlie', '35', 'Paris']
]

# Writing to a CSV file
with csv23.open_csv(file_path, 'w', encoding='utf-8') as writer:
    for row in data_to_write:
        writer.writerow(row)
print(f"Data written to {file_path}")

# Reading from a CSV file
print("Reading data:")
with csv23.open_csv(file_path, 'r', encoding='utf-8') as reader:
    for row in reader:
        print(row)

# Reading as DictReader
print("\nReading data with DictReader:")
with csv23.open_csv(file_path, 'r', rowtype='dict', encoding='utf-8') as dict_reader:
    for row_dict in dict_reader:
        print(row_dict)

# Clean up the dummy file
os.remove(file_path)
print(f"Cleaned up {file_path}")

view raw JSON →