Skip to main content

Latest PyPI Version License Supported Python Versions Wheel

Build Codecov Readthedocs stable Readthedocs latest

csv23 provides the unicode-based API of the Python 3 csv module for Python 2 and 3. Code that should run under both versions of Python can use it to hide the bytes vs. text difference between 2 and 3 and stick to the newer unicode-based interface.

It uses utf-8 as default encoding everywhere.

Improvements

csv23 works around for the following bugs in the stdlib csv module:

bpo-12178

broken round-trip with escapechar if your data contains a literal escape character (fixed in Python 3.10)

bpo-31590

broken round-trip with escapechar and embedded newlines under Python 2 (fixed in Python 3.4 but not backported): produce a warning

Extras

The package also provides some convenience functionality such as the open_csv() context manager for opening a CSV file in the right mode and returning a csv.reader or csv.writer:

>>> import csv23

>>> with csv23.open_csv('spam.csv') as reader:  # doctest: +SKIP
...     for row in reader:
...         print(', '.join(row))
Spam!, Spam!, Spam!'
Spam!, Lovely Spam!, Lovely Spam!'

Python 3 Extras

The read_csv() and write_csv() functions (available on Python 3 only) are most useful if you want (or need to) open a file-like object in the calling code, e.g. when reading or writing directly to a binary stream such as a ZIP file controlled by the caller (emulated with a io.BytesIO below):

>>> import io
>>> buf = io.BytesIO()

>>> import zipfile
>>> with zipfile.ZipFile(buf, 'w') as z, z.open('spam.csv', 'w') as f:
...     csv23.write_csv(f, [[1, None]], header=['spam', 'eggs'])
<zipfile...>

>>> buf.seek(0)
0

>>> with zipfile.ZipFile(buf) as z, z.open('spam.csv') as f:
...     csv23.read_csv(f, as_list=True)
[['spam', 'eggs'], ['1', '']]

csv23 internally wraps the byte stream in a io.TextIOWrapper with the given encoding and newline='' (see csv module docs).

The write_csv()-function also supports updating objects with a .update(<bytes>)-method such as hashlib.new() instances, which allows to calculate a checksum over the binary CSV file output produced from the given rows without writing it to disk (note that the object is returned):

>>> import hashlib

>>> csv23.write_csv(hashlib.new('sha256'), [[1, None]], header=['spam', 'eggs']).hexdigest()
'aed6871f9ca7c047eb55a569e8337af03fee508521b5ddfe7ad0ad1e1139980a'

Both functions have an optional autocompress argument: Set it to True to transparently compress (or decompress) if the file argument is a path that ends in one of '.bz2', '.gz', and '.xz'.

Installation

This package runs under Python 2.7, and 3.7+, use pip to install:

$ pip install csv23

See also

License

This package is distributed under the MIT license.

Release files for csv23 0.3.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for csv23 0.3.4
File Size Uploaded
csv23-0.3.4.zip 42.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for csv23 0.3.4
File Interpreter ABI Platform
csv23-0.3.4-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 61.1 kB

Release files / csv23-0.3.4.zip

Download URL csv23-0.3.4.zip
Size 42.6 kB
Tags Source
SHA-256 checksum
How to use checksums
12faa4b222808fc8963d4b6490a5b42fe3814a0b2c370ffb994944ac62c55a54
BLAKE2b-256 checksum
How to use checksums
4c441dea0214d661e31c8d5bcb76e0b44d07fa852d17965440aabf37dca5a220
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.4

Release files / csv23-0.3.4-py2.py3-none-any.whl

Download URL csv23-0.3.4-py2.py3-none-any.whl
Size 18.5 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
bdf14715b49e151029a3cd1cca953175619fc16356dfb3161432c4028c4436fb
BLAKE2b-256 checksum
How to use checksums
dd88ace08defb77cd3530187048e222b4c55e38bb78917183c07c02b1c8c8b07
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.4

Release history Release notifications | RSS feed

This release

0.3.4 This release

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3

2 release files

0.2

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page