-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_csv.py
More file actions
31 lines (23 loc) · 1.04 KB
/
Copy pathexport_csv.py
File metadata and controls
31 lines (23 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""Exporta a CSV el radar de vencimientos de un sector (CPV) — sin clave.
python examples/export_csv.py 45 vencimientos_construccion.csv
Contratos públicos que caducan pronto y previsiblemente volverán a licitarse:
anticípate a la próxima convocatoria. Datos públicos, no requiere registro.
"""
import csv
import sys
from licitapilot import LicitaPilot
sector = sys.argv[1] if len(sys.argv) > 1 else "45"
out = sys.argv[2] if len(sys.argv) > 2 else f"vencimientos_cpv_{sector}.csv"
lp = LicitaPilot() # capa pública, sin registro
data = lp.vencimientos(sector=sector)
items = data.get("items", [])
if not items:
print(f"Sin vencimientos para el sector {sector}.")
sys.exit(0)
# Volcamos todas las claves que traiga cada fila (esquema estable en la API).
cols = list(items[0].keys())
with open(out, "w", newline="", encoding="utf-8") as f:
w = csv.DictWriter(f, fieldnames=cols, extrasaction="ignore")
w.writeheader()
w.writerows(items)
print(f"Exportados {len(items)} vencimientos de «{data.get('sector', sector)}» a {out}")