MKuranowski / aiocsv

Python: Asynchronous CSV reading/writing
https://pypi.org/project/aiocsv/
MIT License
67 stars 9 forks source link

Force-read `fieldnames` #20

Closed YaraslauZhylko closed 6 months ago

YaraslauZhylko commented 8 months ago

Hi!

The dicumentation for the AsyncDictReader states:

⚠️ Unlike csv.DictReader, if not provided in the constructor, at least one row has to be retrieved before getting the fieldnames.

That means that if I want to validate or manipulate the fieldnames before I start iteratin the rows, I need to either:


Is it possible to add an AsyncDictReader.readheader() method to manually force the fieldnames to be read? Much like AsyncDictWriter.writeheader() writes them to the file:

async def readheader(self) -> None
    if self.fieldnames is None:
        self.fieldnames = await self.reader.__anext__()

So that it can be used like that:

areader = AsyncDictReader(f)
await areader.readheader()
check_or_manipulate(areader.fieldnames)
async for row in areader:
    process(row)

P.S.: I know that the goal of the project is to have a parity with the vanilla csv. But if 100% parity is not technically possible, why not introduce a legitimate workaround?

MKuranowski commented 8 months ago

Yeah, that makes sense. read_header should probably even return the header row, so the usage would simplify to header = await areader.read_header().