⚠️ 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:
read and remember the first row and process it separately:
areader = AsyncDictReader(f)
first_row = await areader.__next__()
check_or_manipulate(areader.fieldnames)
process(first_row)
async for row in areader:
process(row)
use hacks like this:
areader = AsyncDictReader(f)
areader.fieldnames = await areader.reader.__next__()
check_or_manipulate(areader.fieldnames)
async for row in areader:
process(row)
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?
Hi!
The dicumentation for the
AsyncDictReader
states: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 thefieldnames
to be read? Much likeAsyncDictWriter.writeheader()
writes them to the file:So that it can be used like that:
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?