Estructura inicial del proyecto DNIT BOT AUTO
This commit is contained in:
37
app/exportador.py
Normal file
37
app/exportador.py
Normal file
@@ -0,0 +1,37 @@
|
||||
import csv
|
||||
import logging
|
||||
import os
|
||||
|
||||
from config import OUTPUT_DIR, CSV_CHUNK_SIZE
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
ENCABEZADO = ["ruc", "razon_social", "dv", "codigo", "estado", "archivo_origen"]
|
||||
|
||||
|
||||
def exportar_csv(registros, prefijo_nombre="rucs_faltantes"):
|
||||
"""
|
||||
Escribe los registros en archivos CSV de hasta CSV_CHUNK_SIZE filas cada uno.
|
||||
Devuelve la lista de rutas generadas.
|
||||
"""
|
||||
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
||||
|
||||
rutas = []
|
||||
total = len(registros)
|
||||
partes = max(1, (total + CSV_CHUNK_SIZE - 1) // CSV_CHUNK_SIZE)
|
||||
|
||||
for i in range(partes):
|
||||
inicio = i * CSV_CHUNK_SIZE
|
||||
fin = inicio + CSV_CHUNK_SIZE
|
||||
bloque = registros[inicio:fin]
|
||||
|
||||
ruta = os.path.join(OUTPUT_DIR, f"{prefijo_nombre}_part{i + 1}.csv")
|
||||
with open(ruta, "w", newline="", encoding="utf-8") as f:
|
||||
writer = csv.writer(f)
|
||||
writer.writerow(ENCABEZADO)
|
||||
writer.writerows(bloque)
|
||||
|
||||
rutas.append(ruta)
|
||||
logger.info("CSV generado: %s (%d filas)", ruta, len(bloque))
|
||||
|
||||
return rutas
|
||||
Reference in New Issue
Block a user