aiocsv is a Python library for asynchronous CSV reading and writing. It strives to be a drop-in replacement for Python's built-in `csv` module, providing `AsyncReader`, `AsyncDictReader`, `AsyncWriter`, and `AsyncDictWriter` classes. It supports Python 3.9+ and utilizes a C extension for improved performance. The library is actively maintained and currently at version 1.4.0.
pip install aiocsvVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates both reading and writing CSV and TSV files asynchronously using `aiocsv` with `aiofiles`. It covers basic `AsyncReader` and `AsyncWriter` usage for list-based rows, as well as `AsyncDictReader` and `AsyncDictWriter` for dictionary-based rows, including header writing and custom delimiters.
Replace direct access to `AsyncDictReader.fieldnames` with `await reader.get_fieldnames()`.
Ensure the underlying file object provides an `async read` method (e.g., `aiofiles`). Do not pass an `AsyncIterable` of lines.
If `csv.field_size_limit` needs to be changed, do so *before* creating `AsyncReader` instances, or create new `AsyncReader` instances after the change.
Always use `mode='r'` or `mode='w'` and `newline=''` when opening files with `aiofiles` for `aiocsv`.
For very large datasets, consider iterating and writing rows one by one using `writerow()` rather than `writerows()`, or ensure the input iterable is not excessively large.
Install the package using 'pip install aiocsv'.
Ensure the package is up-to-date with 'pip install --upgrade aiocsv' and use 'from aiocsv import AsyncReader'.
Use 'async for row in AsyncReader(afp):' instead of 'for row in AsyncReader(afp):'.
Ensure the package is up-to-date with 'pip install --upgrade aiocsv' and use 'from aiocsv import AsyncDictReader'.
Ensure that all 'aiocsv' operations are performed within the 'async with' context manager.