-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support: Reading from AsyncIterator #27
Comments
Isn't Regardless, yes, there should be support for |
Thanks a lot, you're right, I missed that by focusing on the import asyncio
import codecs
import aioboto3
from aiocsv import AsyncDictReader
from aiocsv.protocols import WithAsyncRead
class AsyncTextReaderWrapper(WithAsyncRead):
def __init__(self, obj, encoding: str, errors="strict"):
self.obj = obj
decoder_factory = codecs.getincrementaldecoder(encoding)
self.decoder = decoder_factory(errors)
async def read(self, size: int) -> str:
raw_data = await self.obj.read(size)
if not raw_data:
return self.decoder.decode(b"", final=True)
return self.decoder.decode(raw_data, final=False)
async def main():
session = aioboto3.Session()
async with session.client("s3") as s3:
response = await s3.get_object(Bucket="my-bucket", Key="data.csv")
f = AsyncTextReaderWrapper(response["Body"], encoding="utf-8")
async for row in AsyncDictReader(f, delimiter=","):
print(row)
if __name__ == '__main__':
asyncio.run(main()) |
Hi, thanks for the work on this library.
I saw in the docs that
aiocsv
only supports reading from file-like objects that support theWithAsyncRead
protocol. However, it would be nice to have the ability to read from anAsyncIterator
, to have feature parity with the standardcsv
lib which can read from an iterable.My specific usecase is reading a CSV from S3 using the
aioboto3
library to stream the file usingiter_lines
oriter_chunks
.Example:
I imagine you could write something to adapt the async iter into the async read API, but I didn't know if you could do this efficiently without buffering the response.
I would be grateful to see this feature considered in the library.
Cheers!
The text was updated successfully, but these errors were encountered: