Registry / serialization / backports-csv

backports-csv

JSON →
library1.0.7pypypi✓ verified 86d ago

backports.csv provides a backport of the Python 3 `csv` module to Python 2 environments. It addresses the significant API differences, particularly concerning string handling and native Unicode support, between Python 2 and Python 3's `csv` modules. The library is currently at version 1.0.7, with its last release on March 10, 2019, suggesting a maintenance-only cadence due to its Python 2 focus.

pip install backports.csv
INSTALL
IMPORT
SIG · BACKPORTS-CSV
B
backports-csv
serializationpythonv1.0.7
Install
1.6s avg
Import
5ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.0.7 · 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.002s · 17.9MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 1.6s · import 0.001s · 18MB
16MB installed
● package 16MB
Code
Verified usage

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

csv
from backports import csv
import backports.csv
The backport makes the Python 3 `csv` module available under the `backports.csv` namespace. To use it as `csv` directly (like in Python 3), import it from the `backports` package.

The quickstart demonstrates reading and writing CSV data using the backported `csv` module. It is crucial to use `io.open` with `newline=''` and specify the `encoding` (e.g., 'utf-8') to handle text files correctly, mirroring Python 3's behavior.

import io from backports import csv # Example: Reading a CSV file def read_csv_file(filename): rows = [] with io.open(filename, newline='', encoding='utf-8') as f: reader = csv.reader(f) for row in reader: rows.append(row) return rows # Example: Writing to a CSV file def write_csv_file(filename, data): with io.open(filename, 'w', newline='', encoding='utf-8') as f: writer = csv.writer(f) for row in data: writer.writerow(row) # To make it runnable for demonstration, create a dummy file dummy_data = [['Header1', 'Header2'], ['Value1', 'Value2'], ['Unicode test', '你好']] write_csv_file('test.csv', dummy_data) read_data = read_csv_file('test.csv') print(read_data)
Debug
Known issues
breakingThe API of the `csv` module in Python 2 (native) is fundamentally different from that in Python 3. This library provides the Python 3 API, meaning code written for Python 2's native `csv` will break if migrated to use `backports.csv` without adaptation.
fix
Rewrite CSV handling code to align with Python 3's `csv` module semantics, particularly regarding file opening and encoding, using `io.open` with `newline=''` and an explicit `encoding`.
affects: Python 2 projects transitioning from native `csv` to `backports.csv`
gotchaProper encoding and newline handling are critical when working with `backports.csv` in Python 2. Unlike Python 2's native `open`, `io.open` must be used with `newline=''` and an explicit `encoding` (e.g., 'utf-8') to correctly handle universal newlines and Unicode characters.
fix
Always open CSV files using `io.open(filename, 'mode', newline='', encoding='utf-8')`. Failure to use `newline=''` can result in incorrect parsing or extra blank rows.
affects: All versions when used in Python 2
Errors
Common errors & fixes
ImportError: cannot import name csv from backports
Attempting to import `csv` directly from `backports` as if `backports` itself were the module `csv`, or trying `from backports.csv import csv` which isn't the correct access pattern.
fix
The correct way to get the `csv` module functionality is `from backports import csv`. This makes the backported module available under the `csv` name.
UnicodeEncodeError: 'ascii' codec can't encode character...
Attempting to write non-ASCII characters without specifying a proper encoding when opening the file, or not using `io.open` with `encoding='utf-8'` (or similar).
fix
Ensure files are opened with `io.open` and an explicit encoding, e.g., `with io.open(filename, 'w', newline='', encoding='utf-8') as f:`.
TypeError: 'str' does not support the buffer interface (or similar encoding-related TypeErrors)
Mixing binary and text modes, or attempting to pass Python 2 `str` (byte strings) to `csv` operations when the backport expects Unicode strings, or vice-versa due to incorrect file opening.
fix
Always use `io.open` with `newline=''` and a specific `encoding` to ensure the `csv` module receives and returns Unicode strings, as it expects Python 3-like text streams.
Upgrade
Version history
1.0.7latest on PyPI · released Mar 11, 2019
Audit
Dependencies

No dependency data recorded yet.

Agent activity
22 hits · last 30 days
node
20
OpenAI (training)
1
Resources
backports-csv — pip install backports-csv · libregistry