Skip to main content

Asynchronous CSV reading/writing

Project description

aiocsv

Asynchronous CSV reading and writing.
AIOFile and Python's csv module combined.

Installation

pip3 install aiocsv

Usage

All objects in aiocsv pass keyword arguments to the underlying csv.reader/csv.writer/... instances.

import asyncio
import csv

from aiofile import AIOFile
from aiocsv import AsyncReader, AsyncDictReader, AsyncWriter, AsyncDictWriter

async def main():
    # simple reading
    async with AIOFile("some_file.csv", mode="r", encoding="utf-8-sig") as afp:
        async for row in AsyncReader(afp):
            print(row)  # row is a list

    # dict reading, tab-separated
    async with AIOFile("some_other_file.tsv", mode="r", encoding="utf-8-sig") as afp:
        async for row in AsyncDictReader(afp, delimiter="\t"):
            print(row)  # row is a dict

    # simple writing, "unix"-dialect
    async with AIOFile("new_file.csv", mode="r", encoding="utf-8-sig") 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 AIOFile("another_new_file.csv", mode="r", encoding="utf-8-sig") 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

AsyncReader

class AsyncReader
 |  AsyncReader(aiofile: aiofile.aio.AIOFile, **csvreaderparams) -> None
 |  
 |  An object that iterates over lines in given aiofile.
 |  Additional keyword arguments are passed to the underlying csv.reader instance.
 |  
 |  Iterating over this object returns parsed rows (List[str]).
 |  
 |  ----------------------------------------------------------------------
 |  Methods defined here:
 |  
 |  __aiter__(self)
 |  
 |  async __anext__(self) -> List[str]
 |  
 |  __init__(self, aiofile: aiofile.aio.AIOFile, **csvreaderparams) -> None
 |      Initialize self.
 |  
 |  ----------------------------------------------------------------------
 |  Readonly properties defined here:
 |  
 |  dialect
 |  
 |  line_num
 |  
 |  ----------------------------------------------------------------------

AsyncDictReader

class AsyncDictReader(builtins.object)
 |  AsyncDictReader(aiofile: aiofile.aio.AIOFile, **csvdictreaderparams) -> None
 |  
 |  An object that iterates over lines in given aiofile.
 |  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 rows (Dict[str, str]).
 |  
 |  ----------------------------------------------------------------------
 |  Methods defined here:
 |  
 |  __aiter__(self)
 |  
 |  async __anext__(self) -> Dict[str, str]
 |  
 |  __init__(self, aiofile: aiofile.aio.AIOFile, **csvdictreaderparams) -> None
 |      Initialize self.
 |  
 |  ----------------------------------------------------------------------
 |  Readonly properties defined here:
 |  
 |  dialect
 |  
 |  line_num
 |  
 |  ----------------------------------------------------------------------

AsyncWriter

class AsyncWriter(builtins.object)
 |  AsyncWriter(aiofile: aiofile.aio.AIOFile, **csvwriterparams) -> None
 |  
 |  An object that writes csv rows to the given aiofile.
 |  In this object "row" is a sequence of values.
 |  
 |  Additional keyword arguments are passed to the underlying csv.writer instance.
 |  
 |  ----------------------------------------------------------------------
 |  Methods defined here:
 |  
 |  __init__(self, aiofile: aiofile.aio.AIOFile, **csvwriterparams) -> None
 |      Initialize self.
 |  
 |  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 defined here:
 |  
 |  dialect
 |  
 |  ----------------------------------------------------------------------

AsyncDictWriter

class AsyncDictWriter(builtins.object)
 |  AsyncDictWriter(aiofile: aiofile.aio.AIOFile, fieldnames: Sequence[str], **csvdictwriterparams) -> None
 |  
 |  An object that writes csv rows to the given aiofile.
 |  In this object "row" is a mapping from fieldnames to values.
 |  
 |  Additional keyword arguments are passed to the underlying csv.DictWriter instance.
 |  
 |  ----------------------------------------------------------------------
 |  Methods defined here:
 |  
 |  __init__(self, aiofile: aiofile.aio.AIOFile, fieldnames: Sequence[str], **csvdictwriterparams) -> None
 |      Initialize self.
 |  
 |  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 defined here:
 |  
 |  dialect
 |  
 |  ----------------------------------------------------------------------

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiocsv-1.0.2.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aiocsv-1.0.2-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file aiocsv-1.0.2.tar.gz.

File metadata

  • Download URL: aiocsv-1.0.2.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2

File hashes

Hashes for aiocsv-1.0.2.tar.gz
Algorithm Hash digest
SHA256 609afb13fe30e965559bae303f0e937f8edd22b00b56e948d09365fe89cf2884
MD5 29aeb1e8421d1aa5656a1b764550ef6d
BLAKE2b-256 86a3f79375c9044254a9ff3e6ae575fc504917667dae82cba87f235a40d718e1

See more details on using hashes here.

File details

Details for the file aiocsv-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: aiocsv-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2

File hashes

Hashes for aiocsv-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f61803ded0b76a0129be0a77f58a2f84f624c448b45bd9d72f8a8e3cacdf7637
MD5 08d21e3c6c3c9bee49620a4c0508fe27
BLAKE2b-256 924b9aa8c80f6dc1a5cb75a70fd4e65ac535c67948a8ef361f19fd8bbdc0164d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page