-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
50 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import asyncio | ||
import csv | ||
|
||
import aiofiles | ||
|
||
|
||
class AsyncCSVIterator: | ||
def __init__(self, path): | ||
self.path = path | ||
self.file = None | ||
|
||
def __aiter__(self): | ||
return self | ||
|
||
async def __anext__(self): | ||
if self.file is None: | ||
self.file = await aiofiles.open(self.path, mode="r") | ||
lines = await self.file.readlines() | ||
self.reader = csv.reader(lines) | ||
try: | ||
return next(self.reader) | ||
except StopIteration: | ||
await self.file.close() | ||
raise StopAsyncIteration | ||
|
||
|
||
async def main(): | ||
csv_iter = AsyncCSVIterator("data.csv") | ||
# Skip the headers | ||
await anext(csv_iter) | ||
# Process the rest of the rows | ||
async for row in csv_iter: | ||
print(row) | ||
|
||
|
||
asyncio.run(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Name,Age,Job | ||
John Doe,30,Software Engineer | ||
Jane Smith,25,Data Scientist | ||
Jim Brown,45,Project Manager | ||
Jessica Jones,40,UX Designer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters