11

I am writing a CSV file with the following function:

import csv
import os
import aiofiles


async def write_extract_file(output_filename: str, csv_list: list):
    """
    Write the extracted content into the file
    """
    try:
        async with aiofiles.open(output_filename, "w+") as csv_file:
            writer = csv.DictWriter(csv_file, fieldnames=columns.keys())
            writer.writeheader()
            writer.writerows(csv_list)
    except FileNotFoundError:
        print("Output file not present", output_filename)
        print("Current dir: ", os.getcwd())
        raise FileNotFoundError

However, as there is no await allowed over writerows method, there are no rows being written into the CSV file.
How to resolve this issue? Is there any workaround available?
Thank you.
Entire code can be found here.

1
  • 4
    note that using async io for local files tends to be slower than synchronous io. you're probably off just using a sync (i.e. non-async) method and wrapping in loop.run_in_executor so that the async code can interact with it nicely Commented Aug 9, 2020 at 9:02

3 Answers 3

8

In my opinion it’s better not to try to use the aiofiles with the csv module and run the synchronous code using loop.run_in_executor and wait it asynchronously like below:

def write_extract_file(output_filename: str, csv_list: list):
    """
    Write the extracted content into the file
    """
    try:
        with open(output_filename, "w+") as csv_file:
            writer = csv.DictWriter(csv_file, fieldnames=columns.keys())
            writer.writeheader()
            writer.writerows(csv_list)
    except FileNotFoundError:
        print("Output file not present", output_filename)
        print("Current dir: ", os.getcwd())
        raise FileNotFoundError


async def main():
    loop = asyncio.get_running_loop()
    await loop.run_in_executor(None, write_extract_file, 'test.csv', csv_list)
Sign up to request clarification or add additional context in comments.

3 Comments

note that this runs write_extract_file synchronously. you need to use await loop.run_in_executor(None, write_extract_file, 'test.csv', csv_list) to actually run it asynchronously
The problem with this approach is if you need to perform async operations in the middle of writing the csv
@royce3 exactly! If I liverage one coroutine for collecting data and another for converting data into a pandas dataframe after some preprocessing and then write to csv, then it will cause problems.
8

You can use aiocsv. Here is a quick example of writing a row to a CSV file asynchronously:

import asyncio
import aiofiles
from aiocsv import AsyncWriter

async def main():
    async with aiofiles.open('your-path.csv', 'w') as f:
        writer = AsyncWriter(f)
        await writer.writerow(['name', 'age'])
        await writer.writerow(['John', 25])

asyncio.run(main())

For more examples follow: https://pypi.org/project/aiocsv/

Comments

-1

You can use aiofiles, you just gotta convert the dict to a row :)

import aiofiles

async def write_extract_file(
    output_filename: str, csv_list: list
):

    cols = columns.keys()

    async with aiofiles.open(output_filename, mode='w+') as f_out:

        await f_out.write(','.join(cols)+'\n')

        for data in csv_list:

            line = []

            for c in cols:
                line.append(str(data[c]) if c in data else '')

            line = ','.join(line) + '\n'
    
            await f_out.write(line)
            
      

1 Comment

This code doesn't handle a lot of the logic that the csv module is performing, like quoting columns that have commas in them for example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.