43 lines
994 B
Python
43 lines
994 B
Python
import asyncio
|
|
import csv
|
|
import aiofiles
|
|
|
|
async def write_rows_to_csv(rows, filename):
|
|
async with aiofiles.open(filename, 'w', newline='') as csvfile:
|
|
writer = csv.writer(csvfile)
|
|
for row in rows:
|
|
await writer.writerow(row)
|
|
|
|
async def main():
|
|
rows = [
|
|
['Name', 'Age', 'Country'],
|
|
['Alice', 30, 'USA'],
|
|
['Bob', 35, 'Canada'],
|
|
['Charlie', 40, 'UK'],
|
|
]
|
|
|
|
await write_rows_to_csv(rows, 'people.csv')
|
|
|
|
asyncio.run(main())
|
|
|
|
# import asyncio
|
|
# import csv
|
|
|
|
# async def write_rows_to_csv(rows, filename):
|
|
# async with open(filename, 'w', newline='') as csvfile:
|
|
# writer = csv.writer(csvfile)
|
|
# for row in rows:
|
|
# await writer.writerow(row)
|
|
|
|
# async def main():
|
|
# rows = [
|
|
# ['Name', 'Age', 'Country'],
|
|
# ['Alice', 30, 'USA'],
|
|
# ['Bob', 35, 'Canada'],
|
|
# ['Charlie', 40, 'UK'],
|
|
# ]
|
|
|
|
# await write_rows_to_csv(rows, 'people.csv')
|
|
|
|
# asyncio.run(main())
|