Asynchronous CSV reading/writing
Project description
aiocsv
Asynchronous CSV reading and writing.
Installation
Python 3.6+ is required.
pip3 install aiocsv
Usage
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.
Reading is implemented using a custom CSV parser, which should behave exactly like the CPython parser.
Writing is implemented using the synchronous csv.writer and csv.DictWriter objects - the serializers write data to a StringIO, and that buffer is then rewritten to the underlaying asynchronous file.
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())
Reference
aiocsv.AsyncReader
AsyncReader(asyncfile: aiocsv.protocols.WithAsyncRead, **csvreaderparams)
An object that iterates over lines in given asynchronous file.
Additional keyword arguments are understood as dialect parameters.
Iterating over this object returns parsed CSV rows (List[str]
).
Methods:
__aiter__(self) -> self
async __anext__(self) -> List[str]
Properties:
dialect
: The csv.Dialect used when parsing
Read-only properties:
line_num
: Not implemented in aiocsv - issues a warning and always returns -1.
aiocsv.AsyncDictReader
AsyncDictReader(asyncfile: aiocsv.protocols.WithAsyncRead,
fieldnames: Optional[Sequence[str]] = None, restkey: Optional[str] = None, restval: Optional[str] = None, **csvreaderparams)
An object that iterates over lines in given asynchronous file.
All arguments work exactly the same like in csv.DictReader.
Iterating over this object returns parsed CSV rows (Dict[str, str]
).
Methods:
__aiter__(self) -> self
async __anext__(self) -> Dict[str, str]
Properties:
fieldnames
: field names used when converting rows to dictionaries
⚠️ Unlike csv.DictReader, if not provided in the constructor, at least one row has to be retrieved before getting the fieldnames.reader = csv.DictReader(some_file) reader.fieldnames # ["cells", "from", "the", "header"] areader = aiofiles.AsyncDictReader(same_file_but_async) areader.fieldnames # ⚠️ None await areader.__anext__() areader.fieldnames # ["cells", "from", "the", "header"]
restkey
: If a row has more cells then the header, all remaining cells are stored under this key in the returned dictionary. Defaults toNone
.restval
: If a row has less cells then the header, then missing keys will use this value. Defaults toNone
.reader
: Underlayingaiofiles.AsyncReader
instance
Read-only properties:
dialect
: Link toself.reader.dialect
- the current csv.Dialectline_num
: Not implemented in aiocsv - issues a warning and always returns -1
aiocsv.AsyncWriter
AsyncWriter(asyncfile: aiocsv.protocols.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:
-
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.protocols.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:
-
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.protocols.WithAsyncRead
A typing.Protocol
describing an asynchronous file, which can be read.
aiocsv.protocols.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.2-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9c936e050c40ec648623917b89e1eb7dc7c1ca5ca65b927097427bf64e2d285b |
|
MD5 | ddac6587040c32fc446a1c2866a5750b |
|
BLAKE2b-256 | f92d15ca74069fd8025fcc6da22a43e9008a21ad95cea78a7cdb7211a3392f31 |
Hashes for aiocsv-1.2.2-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c0bf6c1583a921717891e6a8f3decf60375caef2e266fc890ba98c63a5b32412 |
|
MD5 | fa3073851d3bc715fd553edf7c91db7f |
|
BLAKE2b-256 | 7b97332549d876ea37b3a987f26071325944fcd66c07eab16168cbed89b6050b |
Hashes for aiocsv-1.2.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9811ba6f74013b9969662f3ed3b99b435eaca35be0df84dde2fb8be9b046e621 |
|
MD5 | 810604ebe7a67004927c8207e403c3d0 |
|
BLAKE2b-256 | 85b987e02001868270f3fb549d0274834253cad7539917739b7edde7eb331d50 |
Hashes for aiocsv-1.2.2-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8ec7f5f8918412389cbe8f5a7b3ca06b688d3a16bf7958e9a05f25b3b10fb052 |
|
MD5 | b439b128073d21184458edd9342a1680 |
|
BLAKE2b-256 | fd6a50eae733c471de9c53acb1395bdb964dc7ab43f7774007bf6ce434dabead |
Hashes for aiocsv-1.2.2-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | efe118970ff7ee04b0bcac50347ffbe693a8156d0187942c21e2381a3ec283e5 |
|
MD5 | c250ae4a43baaf1fbb71af4a95f63ecd |
|
BLAKE2b-256 | 0146e6e5ad5fd0ffd9b4e74013864e96b431e69b6761e6e93133486a97f28856 |
Hashes for aiocsv-1.2.2-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1c968ac5a9c9e5dd828a14b47b29f7761a5fbf9561fd529e82cf48dd9c8078ba |
|
MD5 | f25c2ea1ab1e266db6d593912dbe8e99 |
|
BLAKE2b-256 | c053c5efc579845b632ab63bf32e2c926fe275bff0421472e50dea1951499fc4 |
Hashes for aiocsv-1.2.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1fa30dcee9f87099c4afdb2a3c7a16076fd87d57468e9410c4137e110dfbe8c9 |
|
MD5 | 8887f04169ff3c37e78f1de221544473 |
|
BLAKE2b-256 | 87eadff18d4f9ada5d4385e78307ffe0b63492b1b4333ae78b887f8ec7915f82 |
Hashes for aiocsv-1.2.2-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 65f0db9c2a943e303305d8e07bf5d558979e1478d6400a97d7b19574ecea5441 |
|
MD5 | 6fedcc3033043a8956ac8db452c5cd06 |
|
BLAKE2b-256 | 8e6a814c8e9655a20315fe5fa3fc263d6e2710209acf7601902b07778a2b11a7 |
Hashes for aiocsv-1.2.2-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 666454afb2683c37de8d47bd607b1b7fc824ecf8bf760d5d1d79d9444d591054 |
|
MD5 | 75e55460f4fbd474d33c389b420f497b |
|
BLAKE2b-256 | 5a79f2af4ac8841d412fe189eef57fdebe45767d64c7bff98fd93f029169e523 |
Hashes for aiocsv-1.2.2-cp38-cp38-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cd602882acec31fe7a74ad3ac619c82c8adc8fd192809da30e187e8cf1c49b01 |
|
MD5 | 95a9bc0bacd7b2cf83c315d732dec940 |
|
BLAKE2b-256 | 444e72b2acd99c3089cef88207119fbc7723c124766e0b13bc4d523979275398 |
Hashes for aiocsv-1.2.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9d9ffb6f84933d7f6f3d09359e0179aafdf42997f0dd8ebf89cabc1ea18d21ad |
|
MD5 | d8e61e3019f7f996fa6324c7597a71de |
|
BLAKE2b-256 | 9807e6e3383fd9c00cf1fac1982896198ae7290de1f6d641d555a135c2eb39a6 |
Hashes for aiocsv-1.2.2-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8c038ebc30814bd2004c8f32a19074e8363435db4b9fcfac5d796644a971011d |
|
MD5 | caa2233359dba4776e159cd61d284c8f |
|
BLAKE2b-256 | e024525cac9100f42c6afdf533bb2e18fdfc580865036d4124e77e93f21b5734 |
Hashes for aiocsv-1.2.2-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0c7e06bd0a159ea3171e6fd28f739a9aa1c8e07b1a6c731c8d7d09abe6c01286 |
|
MD5 | 56fe810121381d7052aedfe11148ccfc |
|
BLAKE2b-256 | 7b252ba98953d95172b1b8f8501ac6c0bf417475b2c40ca2da9ed99ecfec0159 |
Hashes for aiocsv-1.2.2-cp37-cp37m-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fdd51c9e1b8772d9a2503c38b43098c9d223b7f6afac175dee79b6a85c7b7e23 |
|
MD5 | 863d71ebdc70dd7f53db7ccfe3bf0d4c |
|
BLAKE2b-256 | 2ccf402f8db5aa90c843de2b0bf5ba787bd907167b1cefbde7b6915368f60556 |
Hashes for aiocsv-1.2.2-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c29576ffbba421dcfff630bbf676e07763de66f88c738848224adca90fd9cfe1 |
|
MD5 | e32116ff8eacf5ebcec7e2acea4ba81f |
|
BLAKE2b-256 | b77592c0b7162f7feee54dee1200c6139d1b3a032b749012af71caeaacfa6af1 |
Hashes for aiocsv-1.2.2-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 26bbb0d845eb00d95750c68a0467b1ecde8749be7ddef5102712af895791f19d |
|
MD5 | 2f7f4b757e1b8256016079db5efa6096 |
|
BLAKE2b-256 | 229e147b934517857dd33654cd30543c57089ccf56d5e219cff5fb7e4c9b21ad |
Hashes for aiocsv-1.2.2-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a969cc7bf4fd1cd7ee3939c0855696f57060e7f0ac962717e42968929b3113f0 |
|
MD5 | 5e658f74045f8e78996440947e2331e5 |
|
BLAKE2b-256 | 17ce531855bbb96bc30a5eea1bc661c1ef3972e45f5d6711adbd6ffeb7b122ea |
Hashes for aiocsv-1.2.2-cp36-cp36m-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6b8ed455f2e25f0aa68dbe9cfe37426ea86d8c3047c16f33ef0a238b2a62cb41 |
|
MD5 | 35e3c1fb8e0a3fb191899bcc977a2362 |
|
BLAKE2b-256 | f3f1e760f8d98e82554fe22101babcf21fa7aedce124dee765e81535e9d11280 |
Hashes for aiocsv-1.2.2-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 095dd7851200369eeb95624f1c2bbd0f6917eaed17d0b7b7d9b6a4edb733a7e6 |
|
MD5 | ae6a6f56b7b0b2b24ca883b65ff460d8 |
|
BLAKE2b-256 | dc5ab15d28c8999fae7c6c8cec64644a8c98ff69c6a49a1a3293624e45b5c2c3 |
Hashes for aiocsv-1.2.2-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7e88c5a0e388acc71e506adb9bd0857195962d8dac16037285364f6c02789147 |
|
MD5 | 1707a1fb5629523ae02aba06e3a28510 |
|
BLAKE2b-256 | 79652d2d0b8ec8d21fa32658e0995d98526e79390a8d733c47095c234f34ad75 |