File tree Expand file tree Collapse file tree 5 files changed +50
-10
lines changed
Expand file tree Collapse file tree 5 files changed +50
-10
lines changed Original file line number Diff line number Diff line change 1+ import asyncio
2+ import csv
3+
4+ import aiofiles
5+
6+
7+ class AsyncCSVIterator :
8+ def __init__ (self , path ):
9+ self .path = path
10+ self .file = None
11+
12+ def __aiter__ (self ):
13+ return self
14+
15+ async def __anext__ (self ):
16+ if self .file is None :
17+ self .file = await aiofiles .open (self .path , mode = "r" )
18+ lines = await self .file .readlines ()
19+ self .reader = csv .reader (lines )
20+ try :
21+ return next (self .reader )
22+ except StopIteration :
23+ await self .file .close ()
24+ raise StopAsyncIteration
25+
26+
27+ async def main ():
28+ csv_iter = AsyncCSVIterator ("data.csv" )
29+ # Skip the headers
30+ await anext (csv_iter )
31+ # Process the rest of the rows
32+ async for row in csv_iter :
33+ print (row )
34+
35+
36+ asyncio .run (main ())
Original file line number Diff line number Diff line change @@ -12,11 +12,10 @@ async def stream_generator(files):
1212
1313
1414async def main (directory , zip_name = "output.zip" ):
15- files = [{"file" : file } for file in directory .iterdir ()]
16-
17- async with aiofiles .open (zip_name , mode = "wb" ) as z :
15+ files = [{"file" : path } for path in directory .iterdir () if path .is_file ()]
16+ async with aiofiles .open (zip_name , mode = "wb" ) as archive :
1817 async for chunk in stream_generator (files ):
19- await z .write (chunk )
18+ await archive .write (chunk )
2019
2120
2221directory = Path ()
Original file line number Diff line number Diff line change 1+ Name,Age,Job
2+ John Doe,30,Software Engineer
3+ Jane Smith,25,Data Scientist
4+ Jim Brown,45,Project Manager
5+ Jessica Jones,40,UX Designer
Original file line number Diff line number Diff line change 44
55
66class AsyncFileIterable :
7- def __init__ (self , filename , chunk_size = 1024 ):
8- self .filename = filename
7+ def __init__ (self , path , chunk_size = 1024 ):
8+ self .path = path
99 self .chunk_size = chunk_size
1010
1111 async def __aiter__ (self ):
12- async with aiofiles .open (self .filename , mode = "rb" ) as file :
12+ async with aiofiles .open (self .path , mode = "rb" ) as file :
1313 while True :
1414 chunk = await file .read (self .chunk_size )
1515 if not chunk :
Original file line number Diff line number Diff line change 44
55
66class AsyncFileIterator :
7- def __init__ (self , filename , chunk_size = 1024 ):
8- self .filename = filename
7+ def __init__ (self , path , chunk_size = 1024 ):
8+ self .path = path
99 self .chunk_size = chunk_size
1010 self .file = None
1111
@@ -14,7 +14,7 @@ def __aiter__(self):
1414
1515 async def __anext__ (self ):
1616 if self .file is None :
17- self .file = await aiofiles .open (self .filename , mode = "rb" )
17+ self .file = await aiofiles .open (self .path , mode = "rb" )
1818 chunk = await self .file .read (self .chunk_size )
1919 if not chunk :
2020 await self .file .close ()
You can’t perform that action at this time.
0 commit comments