gpt4 book ai didi

python - 在 Python 中异步编写 CSV 文件

转载 作者:行者123 更新时间:2023-12-04 00:54:51 24 4
gpt4 key购买 nike

我正在编写具有以下功能的 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 文件。
如何解决这个问题?有没有可用的解决方法?
谢谢你。
完整代码可见 here .

最佳答案

在我看来,最好不要尝试使用 aiofilescsv模块并使用 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)

关于python - 在 Python 中异步编写 CSV 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63324327/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com