猿问

在 Python 中异步编写 CSV 文件

我正在编写具有以下功能的 CSV 文件:


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

但是,由于不允许等待writerows方法,因此没有行被写入 CSV 文件。

如何解决这个问题?有什么解决方法吗?

谢谢。完整的代码可以在这里

拉莫斯之舞
浏览 430回答 3
3回答

慕码人8056858

在我看来,最好不要尝试与模块aiofiles一起使用csv并运行同步代码loop.run_in_executor并异步等待它,如下所示: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 FileNotFoundErrorasync def main():    loop = asyncio.get_running_loop()    await loop.run_in_executor(None, write_extract_file, 'test.csv', csv_list)

摇曳的蔷薇

您可以使用aiocsv。以下是将一行异步写入 CSV 文件的快速示例:import asyncioimport aiofilesfrom aiocsv import AsyncWriterasync 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())

天涯尽头无女友

你可以使用 aiofiles,你只需要将 dict 转换为一行 :)import aiofilesasync 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)
随时随地看视频慕课网APP

相关分类

Python
我要回答