2026-09-15 16:54:29 -03:00
|
|
|
import csv
|
|
|
|
|
import logging
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
from config import OUTPUT_DIR, CSV_CHUNK_SIZE
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
2026-09-16 14:24:35 -03:00
|
|
|
ENCABEZADO = ["ruc", "razon_social", "dv", "codigo", "estado"]
|
2026-09-15 16:54:29 -03:00
|
|
|
|
|
|
|
|
|
2026-09-16 14:24:35 -03:00
|
|
|
def exportar_csv(registros, prefijo_nombre="rucs_faltantes", encabezado=ENCABEZADO):
|
2026-09-15 16:54:29 -03:00
|
|
|
"""
|
|
|
|
|
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)
|
2026-09-16 14:24:35 -03:00
|
|
|
writer.writerow(encabezado)
|
2026-09-15 16:54:29 -03:00
|
|
|
writer.writerows(bloque)
|
|
|
|
|
|
|
|
|
|
rutas.append(ruta)
|
|
|
|
|
logger.info("CSV generado: %s (%d filas)", ruta, len(bloque))
|
|
|
|
|
|
|
|
|
|
return rutas
|