为了账号安全,请及时绑定邮箱和手机立即绑定

在 Python 中异步编写 CSV 文件

在 Python 中异步编写 CSV 文件

拉莫斯之舞 2023-05-09 10:31:46
我正在编写具有以下功能的 CSV 文件:import csvimport osimport aiofilesasync 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 文件。如何解决这个问题?有什么解决方法吗?谢谢。完整的代码可以在这里
查看完整描述

3 回答

?
慕码人8056858

TA贡献1803条经验 获得超6个赞

在我看来,最好不要尝试与模块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 FileNotFoundError



async def main():

    loop = asyncio.get_running_loop()

    await loop.run_in_executor(None, write_extract_file, 'test.csv', csv_list)


查看完整回答
反对 回复 2023-05-09
?
摇曳的蔷薇

TA贡献1793条经验 获得超6个赞

您可以使用aiocsv。以下是将一行异步写入 CSV 文件的快速示例:

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())


查看完整回答
反对 回复 2023-05-09
?
天涯尽头无女友

TA贡献1831条经验 获得超9个赞

你可以使用 aiofiles,你只需要将 dict 转换为一行 :)


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)


查看完整回答
反对 回复 2023-05-09
  • 3 回答
  • 0 关注
  • 429 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信