Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

403
Views
Escribir un archivo CSV de forma asíncrona en Python

Estoy escribiendo un archivo CSV con la siguiente función:

 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

Sin embargo, como no se permite la espera sobre el método writerows , no se escriben filas en el archivo CSV.
Cómo resolver este problema? ¿Hay alguna solución disponible?
Gracias.
El código completo se puede encontrar aquí .

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

En mi opinión, es mejor no intentar usar los aiofiles con el módulo csv y ejecutar el código síncrono usando loop.run_in_executor y esperarlo de forma asíncrona como se muestra a continuación:

 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)
over 4 years ago · Santiago Trujillo Report

0

Puedes usar aiocsv . Aquí hay un ejemplo rápido de cómo escribir una fila en un archivo CSV de forma asíncrona:

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

Para ver más ejemplos, siga: https://pypi.org/project/aiocsv/

over 4 years ago · Santiago Trujillo Report

0

Puedes usar aiofiles, solo tienes que convertir el dict en una fila :)

 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)
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!