No project description provided
Project description
aiocsv
Asynchronous CSV reading and writing.
Installation
pip install aiocsv
. Python 3.8+ is required.
This module contains an extension written in C. Pre-build binaries may not be available for your configuration. You might need a C compiler and Python headers to 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 underlying 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())
Differences with csv
aiocsv
strives to be a drop-in replacement for Python's builtin
csv module. However, there are 3 notable differences:
- Readers accept objects with async
read
methods, instead of an AsyncIterable over lines from a file. AsyncDictReader.fieldnames
can beNone
- useawait AsyncDictReader.get_fieldnames()
instead.- Changes to
csv.field_size_limit
are not picked up by existing Reader instances. The field size limit is cached on Reader instantiation to avoid expensive function calls on each character of the input.
Reference
aiocsv.AsyncReader
AsyncReader(asyncfile: aiocsv.protocols.WithAsyncRead, **csvreaderparams)
An object that iterates over records in the given asynchronous CSV 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]
Read-only properties:
dialect
: The csv.Dialect used when parsingline_num
: The number of lines read from the source file. This coincides with a 1-based index of the line number of the last line of the recently parsed record.
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 records in the given asynchronous CSV file. All arguments work exactly the same was as in csv.DictReader.
Iterating over this object returns parsed CSV rows (Dict[str, str]
).
Methods:
__aiter__(self) -> self
async __anext__(self) -> Dict[str, str]
async get_fieldnames(self) -> List[str]
Properties:
fieldnames
: field names used when converting rows to dictionaries
⚠️ Unlike csv.DictReader, this property can't read the fieldnames if they are missing - it's not possible toawait
on the header row in a property getter. Useawait reader.get_fieldnames()
.reader = csv.DictReader(some_file) reader.fieldnames # ["cells", "from", "the", "header"] areader = aiofiles.AsyncDictReader(same_file_but_async) areader.fieldnames # ⚠️ None await areader.get_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
: Underlyingaiofiles.AsyncReader
instance
Read-only properties:
dialect
: Link toself.reader.dialect
- the current csv.Dialectline_num
: The number of lines read from the source file. This coincides with a 1-based index of the line number of the last line of the recently parsed record.
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.
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.
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.3.0-cp312-cp312-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a3fb7eb902ccb4a97175f23bf608890b27cb2290ce24493285b0ec66e70e6e1f |
|
MD5 | 62c365ef0b247c7aba0c4d1bbbb45404 |
|
BLAKE2b-256 | 262daf27e9bd18f6eba1cbf924f2cdacb3fbbb2711164096804529bca8b087d6 |
Hashes for aiocsv-1.3.0-cp312-cp312-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 99fc00625d90beceac0db1709f410b412de582dc477f3410999c6d5a5d9a6e1a |
|
MD5 | ed2e5052b48427b59a2cadc3afc6ab00 |
|
BLAKE2b-256 | 4cb6ea5dcee1448955c215f8dc720aa9cc61064a5ee96518f90814a00fab8ea3 |
Hashes for aiocsv-1.3.0-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 85d267ebc4745b609f284bcc44d2ed29a548222d4e65920f8c74c25302557bd8 |
|
MD5 | 0e273cb91a9f7c98674740523225ea16 |
|
BLAKE2b-256 | c0444f6dce64d983873072898d45362d5cc90f8be41791a250293919acbcd3e5 |
Hashes for aiocsv-1.3.0-cp312-cp312-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dbd1c81a65d0da60db7548ee950fba0f4363e5cf56909672321e0e6f768c552f |
|
MD5 | 11d5ba0b3dbcd17dc2087194aafed90a |
|
BLAKE2b-256 | 2e3f2a5c22b72d995bbaac68a23b5f21f479866f5af58365df8306a1acd76e52 |
Hashes for aiocsv-1.3.0-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f20cde0e370014d471fb9e93a1269d2b1db8d0af50a62a8dcd00f3ae29f92008 |
|
MD5 | d4ba83be1a32e4e5eab47283ad80d6f9 |
|
BLAKE2b-256 | 2afb55f5edfc51600d4f348a4720e03257a9ed92c92b612c8fd2642ed8ffe15b |
Hashes for aiocsv-1.3.0-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7327971c310b56dd4481849223b69c3239fc07c12a64cb07f445c7250f3b644f |
|
MD5 | 3bb79316ee1468bfc4270de9a6a91443 |
|
BLAKE2b-256 | 5bedc3beffa0a485a63a105473c0d62c1a640899837a7ee4ff4ebddc1e5e4dc3 |
Hashes for aiocsv-1.3.0-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 95c7f8de419732bda5a28256cfb0f024194fab2d66f676ffa7023df12e4c9263 |
|
MD5 | a949dc4effa2eed2c2df324d32dc605f |
|
BLAKE2b-256 | 212f201565ecde92c4f5d67b8e3bfcf97a0985d41c9753e2604e9e46c8696e1e |
Hashes for aiocsv-1.3.0-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bf146c7d88f37c9d81a182add28418cb5bd7a31495fce87afdccb614239f44ed |
|
MD5 | 1a1469c9a158fe7c8afdca83e1ccede9 |
|
BLAKE2b-256 | edd034b5909de8f0e3c0f493de462273ddaafda058d610b831d53b7bed2e1a06 |
Hashes for aiocsv-1.3.0-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 70ebe4ec79ee499b1fb782316730d20817748a5a84ff38b9c8a8184b6e167af9 |
|
MD5 | 745c6259de169560c2c83dc5f0708df0 |
|
BLAKE2b-256 | 1dab208e6e820e19a65dc10d6b7cdb99720c8e35faaf72a2a1963cfe2d8e1822 |
Hashes for aiocsv-1.3.0-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 861c11fe02a2105121a6dd3338a526576e6807c730c1245608c4b87c2d0805c7 |
|
MD5 | 4dac0094a8915d5168927da8d325524d |
|
BLAKE2b-256 | 45c93ee6208490012a3f52981f0066ff3a29a2b27c4d35bcc0da21f7a3050393 |
Hashes for aiocsv-1.3.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ce3b4cb687060255f1a1919d6534e663da1a588cd7b1917768a513f6b4f40043 |
|
MD5 | 1becdc6336f62e34fea8f51e5fdb69f5 |
|
BLAKE2b-256 | 2ff35741d63a8868c5bb528127624527b1fcd4af297e1135cdbe77f9f44eedd4 |
Hashes for aiocsv-1.3.0-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b65ae59c5b9f056563e7b22832fe7ebe56edf9be65c88086719e0a4c050f704a |
|
MD5 | 984c88026a1183e2903c92a69820658b |
|
BLAKE2b-256 | 15c23be617c2668bd3cf945637bbfb1a1c7c5b17cecc7f54cb46d5650484333e |
Hashes for aiocsv-1.3.0-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ece20b0f43ba6b11f8429f914caaaa0dec85e0be852f96c83b8a9ec7752bf96c |
|
MD5 | c31d152e4a631bff2249c2f95c205234 |
|
BLAKE2b-256 | 6e5cd654d602deff9287e0841a048330b2bd7ac890495d88190a82114d0e2cf2 |
Hashes for aiocsv-1.3.0-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bd3c2c498e86ae48e7eb2aaad420fbf2a14f2f6eff541f45a86fabff83180dc7 |
|
MD5 | 2427df53e2d19afe325779cc2e40fe24 |
|
BLAKE2b-256 | f0dcefa959211154de446e82d2213e7c8cb14e6a2b5989d5bd64d414eca4d349 |
Hashes for aiocsv-1.3.0-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 37584223efe4bfd23ff215f387076b5a985b36d7fc974b3dec93378f3f547ec5 |
|
MD5 | 746e334e11b85f5d86b6513ed3b18bf6 |
|
BLAKE2b-256 | 1fd3719dd324e7f5ff64a28054783fa88b51ff0fd16fbced461270e330ccc645 |
Hashes for aiocsv-1.3.0-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 699854008b412da2c65273574a7dd26c8ea307578018517c28972e256155ee90 |
|
MD5 | 38f8960544af93f63303b49d1bdc5792 |
|
BLAKE2b-256 | 10eb080e1316e330a68aea2a4be8b20ab2a938f4515033d9da03b3df9b83ea94 |
Hashes for aiocsv-1.3.0-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 558722c1c5e44553a7ebabf48375b44fbfd0a12cfbd83da1993b97827eee7c14 |
|
MD5 | a83e99231303d3ea5626daffb6afaa42 |
|
BLAKE2b-256 | fb44456c125b04d3a4c5653c4ece5d8c0121e46974657b135b70a94b021d0132 |
Hashes for aiocsv-1.3.0-cp38-cp38-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 086a85f63447d547badb166dc7039ea1f7ad36bcb932910edca7c8ca0a30f3f2 |
|
MD5 | 5fe9971be83e919336aa4039dcd7d4e8 |
|
BLAKE2b-256 | 7433e85d98a8c0bf850b8475638776ecaca93481f589d61d8b37bdd0ceb57feb |
Hashes for aiocsv-1.3.0-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e48b36fb26303fd57c02d8936a3adedd68170029dfa0495c020ba81e11781982 |
|
MD5 | 20618428af94c54858e3b64e87f8c3a8 |
|
BLAKE2b-256 | 8119d51ed7f0b666ff1c92edb5d28491725163d88c5464fd7aa3d17358ee99d1 |
Hashes for aiocsv-1.3.0-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ef37d6a55a3e58be871fdd278fd362955b63dba11ed5b46e339c8b3a2bda4294 |
|
MD5 | 3f74eafb76f15c8886a3b18953829845 |
|
BLAKE2b-256 | 094db9a3046243386786a57e33c3b64467086e607cb529f0aeb7feffbde4d212 |