Asynchronous CSV reading/writing
Project description
aiocsv
Asynchronous CSV reading and writing.
Installation
Python 3.6+ is required.
pip3 install aiocsv
Usage
All aiocsv classes behave (almost, see below) like their synchronous counterparts from the csv module - after all aiocsv is only a wrapper around the synchronous objects.
The only different behavior is in newline handling. Since aiocsv buffers files row-by-row,
underlaying streams must be opened with newline=""
and must use the same line terminators
as defined by Dialect.lineterminator (default: CRLF).
The built-in csv mosule usually handles mismatched newlies ok-ish, whereas aiocsv will most likely break.
AsyncReader & AsyncDictReader accept any object that has a read(size: int)
coroutine,
which should return a string.
AsyncWriter & AsyncDictWriter accept any object that has a write(b: str)
coroutine.
All objects in aiocsv
pass keyword arguments to the underlying
csv.reader/csv.writer/... instances.
Example
Example usage with aiofiles.
import asyncio
import csv
import aiofiles
from aiocsv import AsyncReader, AsyncDictReader, AsyncWriter, AsyncDictWriter
async def main():
# simple reading
async with aiofiles.open("some_file.csv", mode="r", encoding="utf-8", newline="") as afp:
async for row in AsyncReader(afp):
print(row) # row is a list
# dict reading, tab-separated
async with aiofiles.open("some_other_file.tsv", mode="r", encoding="utf-8", newline="") as afp:
async for row in AsyncDictReader(afp, delimiter="\t"):
print(row) # row is a dict
# simple writing, "unix"-dialect
async with aiofiles.open("new_file.csv", mode="w", encoding="utf-8", newline="") as afp:
writer = AsyncWriter(afp, dialect="unix")
await writer.writerow(["name", "age"])
await writer.writerows([
["John", 26], ["Sasha", 42], ["Hana", 37]
])
# dict writing, all quoted, "NULL" for missing fields
async with aiofiles.open("new_file2.csv", mode="w", encoding="utf-8", newline="") as afp:
writer = AsyncDictWriter(afp, ["name", "age"], restval="NULL", quoting=csv.QUOTE_ALL)
await writer.writeheader()
await writer.writerow({"name": "John", "age": 26})
await writer.writerows([
{"name": "Sasha", "age": 42},
{"name": "Hana"}
])
asyncio.run(main())
Caching
AsyncReader / AsyncDictReader will read a set amount of bytes from the provided stream, cache it in a io.StringIO. This StringIO is then consumed by the underlying csv.reader / csv.DictReader instances.
By default 1024 bytees are read from the stream,
you can change this value by setting aiocsv.READ_SIZE
.
AsyncWriter / AsyncDictWriter will follow provided row(s) to their underlying csv.writer / csv.DictWriter instances. They output produced CSV rows into a io.StringIO, which is then rewritten to the actual stream.
Reference
aiocsv.AsyncReader
AsyncReader(asyncfile: aiocsv._WithAsyncRead, **csvreaderparams)
An object that iterates over lines in given asynchronous file.
Additional keyword arguments are passed to the underlying csv.reader instance.
Iterating over this object returns parsed CSV rows (List[str]
).
Methods:
__aiter__(self) -> self
async __anext__(self) -> List[str]
__init__(self, asyncfile: aiocsv._WithAsyncRead, **csvreaderparams) -> None
Readonly properties:
dialect
: Link to underlying's csv.reader'sdialect
attributeline_num
: Link to underlying's csv.reader'sline_num
attribute
aiocsv.AsyncDictReader
AsyncDictReader(asyncfile: aiocsv._WithAsyncRead, **csvdictreaderparams)
An object that iterates over lines in given asynchronous file.
Additional keyword arguments are passed to the underlying csv.DictReader instance.
If given csv file has no header, provide a 'fieldnames' keyword argument,
like you would to csv.DictReader.
Iterating over this object returns parsed CSV rows (Dict[str, str]
).
Methods:
__aiter__(self) -> self
async __anext__(self) -> Dict[str, str]
__init__(self, asyncfile: aiocsv._WithAsyncRead, **csvdictreaderparams) -> None
Readonly properties:
dialect
: Link to underlying's csv.reader'sdialect
attributeline_num
: Link to underlying's csv.reader'sline_num
attribute
aiocsv.AsyncWriter
AsyncWriter(asyncfile: aiocsv._WithAsyncWrite, **csvwriterparams)
An object that writes csv rows to the given asynchronous file.
In this object "row" is a sequence of values.
Additional keyword arguments are passed to the underlying csv.writer instance.
Methods:
-
__init__(self, asyncfile: aiocsv._WithAsyncWrite, **csvwriterparams) -> None
-
async writerow(self, row: Iterable[Any]) -> None
Writes one row to the specified file. -
async writerows(self, rows: Iterable[Iterable[Any]]) -> None
Writes multiple rows to the specified file.All rows are temporarly stored in RAM before actually being written to the file,
so don't provide a generator of loads of rows.
Readonly properties:
dialect
: Link to underlying's csv.reader'sdialect
attribute
aiocsv.AsyncDictWriter
AsyncDictWriter(asyncfile: aiocsv._WithAsyncWrite, fieldnames: Sequence[str], **csvdictwriterparams)
An object that writes csv rows to the given asynchronous file.
In this object "row" is a mapping from fieldnames to values.
Additional keyword arguments are passed to the underlying csv.DictWriter instance.
Methods:
-
__init__(self, asyncfile: aiocsv._WithAsyncWrite, fieldnames: Sequence[str], **csvdictwriterparams) -> None
-
async writeheader(self) -> None
Writes header row to the specified file. -
async writerow(self, row: Mapping[str, Any]) -> None
Writes one row to the specified file. -
async writerows(self, rows: Iterable[Mapping[str, Any]]) -> None
Writes multiple rows to the specified file.All rows are temporarly stored in RAM before actually being written to the file, so don't provide a generator of loads of rows.
Readonly properties:
dialect
: Link to underlying's csv.reader'sdialect
attribute
aiocsv.READ_SIZE
(int
); Amout of bytes to be read when consuming streams in Reader instances.
aiocsv._WithAsyncRead
A typing.Protocol
describing an asynchronous file, which can be read.
aiocsv._WithAsyncWrite
A typing.Protocol
describing an asynchronous file, which can be written to.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for aiocsv-1.2.1-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8bb878b4a753067390c31f756bf55798e3fc71fa3385a1981174ece1a405728a |
|
MD5 | 7b402665b505bd21ad14d75ec61b9adc |
|
BLAKE2b-256 | 8fa453bbc3a6f0d1beb940c87b5fd230ed6c4ba3da0dd05f3c3576ed78ae02ef |
Hashes for aiocsv-1.2.1-cp39-cp39-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 457f05a74fcb2bfb5ec21a4119fb3eab6ce0fb2a56505eaf87680ca68ce15de3 |
|
MD5 | 46529b8cb224ad6a029e91a3840cbf46 |
|
BLAKE2b-256 | 3084c665a8a12c5c63cd178af2a987b7859791ff9d7cd6f46b6a07ea0da25ab3 |
Hashes for aiocsv-1.2.1-cp39-cp39-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8b489e66ffaefc4e299884f63478e81334816ebfc931724fea1a04ab99f2ec52 |
|
MD5 | 963beac3a5bc44874d936812c5146dfb |
|
BLAKE2b-256 | d6c71905ce87569ee75cf5d60b7a574923dabfbecade5589f07bf1f6d49d2715 |
Hashes for aiocsv-1.2.1-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ad948a278843efd70aa77520ae4c8afa8fb684193e57a59f14e3e5f8c37e979b |
|
MD5 | 8ece807b9dc404f4d6065f9f7a420e7a |
|
BLAKE2b-256 | b63af03b620bc514a7922300f6cbc1a09e98536105338238963530bec6fe0b09 |
Hashes for aiocsv-1.2.1-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a64ceb6a8a23b0a5b50542ab7e57f368236a48bf3dc4288d218996345f478847 |
|
MD5 | c71ab99aaac4b89ec05af61085cc7c66 |
|
BLAKE2b-256 | ebb2bb7c29f8877aea5bd7260d2a1ff1e03f35a22b9c1d3b4a7be4cd07f52931 |
Hashes for aiocsv-1.2.1-cp38-cp38-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c257bdb79498d6fb3fac6d0e036ecd713826271cd0f8d3f88fc4db2587fcfda3 |
|
MD5 | 194d41251c673074f7fca146f1fd0e11 |
|
BLAKE2b-256 | ef006c5b7c3b4d05b7465cec410b255755e5c25608a2d5b234c78c8ee7f46d23 |
Hashes for aiocsv-1.2.1-cp38-cp38-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 009350323237c89d52022f8d50d8b3219081baeef61aa23e768fe00b5e7d0c9f |
|
MD5 | 3357d318d8d382ef202ce89f74114ec4 |
|
BLAKE2b-256 | 4491b9809461b68701d82c8bc8492beec06402b871bd8d84333b9c739db3c02c |
Hashes for aiocsv-1.2.1-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d810105dd0588567890f2849ad32f33c664e8deeb510f8dbfc4e29d7f579629d |
|
MD5 | 0b77d6e06c11b5b5a3109202a662dbcc |
|
BLAKE2b-256 | e4903df45282b37d48f88363fae711b7297a2dd1aa7adfa1c83e198a2fc03e11 |
Hashes for aiocsv-1.2.1-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a5cbec0d71e559917327b63bbbd9ae813f818f25bcb291ac68f561df0c725f08 |
|
MD5 | ee975273849a39cf67db655080f2fb84 |
|
BLAKE2b-256 | 6eda6285dd13cb93f031348ac9ac136979578843dd9bf1b8a7bc89d2737e5330 |
Hashes for aiocsv-1.2.1-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3ddd184f6c27cd05af7710c71b877e26c3b7cb32706ac18ba91febf6a1667f09 |
|
MD5 | 76db450908f773afba2e1f74f84e598d |
|
BLAKE2b-256 | 563d950c68988f3477c8ee6d79913b22590be575e59d590414c851f48974d597 |
Hashes for aiocsv-1.2.1-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c0116f53bf1451dbdb1ed4e538d51938ecb03f9d84239d27bd01a598ae7a31bd |
|
MD5 | 35191a28020d8cb184d1ae54a4e765b4 |
|
BLAKE2b-256 | c7a5c668b73627c1db4fac3956dca44359e9c0598bc7d66a29e1a57e913175b8 |
Hashes for aiocsv-1.2.1-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b5eaccd202c796b1689f1364585a2efd79a0c60fe3d31f700e2bfb33bdb8df06 |
|
MD5 | 08e138c822cc4854b849af3518e5e157 |
|
BLAKE2b-256 | 22adbae616e2bafb9b105ecf7dec22d18dc8958421fd66c3da87b0082246c921 |
Hashes for aiocsv-1.2.1-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f23c05192faddb29f6949a86d771c22236f04db56fa950c94c0a21c6c9a99e26 |
|
MD5 | 63900c43ada8a5a7ebc7f48674495150 |
|
BLAKE2b-256 | 2635669488c59a9569dfed35959e75c2bd980e9f6db16fb8e1d2ccf551a250a0 |
Hashes for aiocsv-1.2.1-cp36-cp36m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 722b43b0658a3f8105874663841ffd47f29ad956b864d3524923a146ceacedcc |
|
MD5 | 07a50bcb95194dde4aa784567abe278b |
|
BLAKE2b-256 | 4daf15cb46c35a43586538f14057330ea73aefd5228d0c34cd690ac3d5297111 |
Hashes for aiocsv-1.2.1-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 21ec5e053f03a54ec6819a0d96fdc6fe2c20aff7855038bfe3f6bb21c10a3d04 |
|
MD5 | a2e6148d015bd8e157abb5f02447085e |
|
BLAKE2b-256 | 12e97cc415a6f25cb7a3cf36d8758a6014309285c7cbaec81b559b3a775023be |
Hashes for aiocsv-1.2.1-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dc1898ae0c967f943d5490d0ed4eea6bc190ea2628c581a3bd46fc642929d704 |
|
MD5 | 52ddec2513b7338f3498f1f092cb8065 |
|
BLAKE2b-256 | 41e0331a08b82f97de7b88da338e07f17a0b6a517c44a0b66fa7d85f8813dd6c |