Registry / data / csv23
library0.3.4pypypi✓ verified 86d ago

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.

pip install csv23
INSTALL
IMPORT
SIG · CSV23
C
csv23
datapythonv0.3.4
Install
1.6s avg
Import
394ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.3.4 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.426s · 17.9MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 1.6s · import 0.363s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

open_csv
from csv23 import open_csv
import csv
While `csv23` mimics the standard `csv` module, direct import of `csv` won't provide the compatibility layer. Use `csv23.open_csv` for the primary context manager.
reader
from csv23 import reader
For direct access to the `reader` class.
writer
from csv23 import writer
For direct access to the `writer` class.
DictReader
from csv23 import DictReader
For reading CSV data into dictionaries.
DictWriter
from csv23 import DictWriter
For writing dictionaries to CSV.

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}")
Debug
Known issues
breakingVersion 0.3.4 is the last release to officially support Python 2. Future versions are Python 3 only.
fix
For Python 2 compatibility, pin to `csv23==0.3.4`. For new projects targeting only Python 3, `csv23` still offers useful wrappers but direct `csv` module use might suffice.
affects: >=0.3.5
gotchaWhen using `csv23.DictWriter`, the `fieldnames` parameter is mandatory if `mode='w'` is used, similar to the standard `csv.DictWriter`.
fix
Always provide the `fieldnames` keyword argument when initializing `DictWriter` or `open_csv(..., rowtype='dict', mode='w', fieldnames=...)`.
affects: All versions
gotcha`csv23.NamedTupleReader` automatically uses the first row as field names. These names must be valid Python identifiers. If not, consider using `rename=True` or `rowtype='list'`.
fix
If header names contain special characters (e.g., spaces, hyphens) which are invalid Python identifiers, pass `rename=True` to `open_csv` when `rowtype='namedtuple'` to automatically convert them to valid names (e.g., `_0`, `_1`).
affects: All versions
Errors
Common errors & fixes
UnicodeDecodeError: 'codec' can't decode byte ... in position ...: invalid start byte
The CSV file is encoded differently than the encoding specified (or defaulted to 'utf-8'). This is a common issue with CSV files from various sources.
fix
Specify the correct `encoding` parameter when opening the file, e.g., `csv23.open_csv('file.csv', encoding='latin1')`. If unsure, try common encodings like 'latin1', 'cp1252', or use a library like `chardet` to detect it. You can also specify `errors='replace'` or `errors='ignore'` in the `open_csv` call to handle problematic characters.
TypeError: DictWriter requires a 'fieldnames' parameter.
You are attempting to write to a CSV file using `csv23.DictWriter` (or `open_csv` with `rowtype='dict'` and `mode='w'`) without providing the `fieldnames` argument.
fix
Provide a list of column headers as the `fieldnames` argument when creating the `DictWriter` or calling `open_csv`. For example: `with csv23.open_csv('output.csv', 'w', rowtype='dict', fieldnames=['Header1', 'Header2']) as writer:`.
NotImplementedError: If encoding is not 8-bit clean.
This error can occur in `csv23` (especially under Python 2) if an encoding that is not 8-bit clean (e.g., `utf-16`) is specified, as the library's internal optimizations for certain encodings might not support it.
fix
Use an 8-bit clean encoding like 'utf-8' (default) or 'latin-1' if possible. If a multi-byte encoding like 'utf-16' is truly required, you might need to handle the file opening with `io.open` or similar directly, then pass the file-like object to `csv23.reader`/`writer` (though this might bypass some `csv23` benefits).
Upgrade
Version history
0.3.4latest on PyPI · released Jun 12, 2022
Audit
Dependencies
pythonrequiredExplicitly designed for Python 2.7+ and 3.x (excluding 3.0-3.6 for direct support in v0.3.4, though it provides Python 3 CSV API). Future versions beyond 0.3.4 are Python 3 only.
Agent activity
18 hits · last 30 days
node
16
OpenAI (training)
1
Resources
csv23 — pip install csv23 · libregistry