git pyfleet en gitea.xiberus
This commit is contained in:
Executable
+47
@@ -0,0 +1,47 @@
|
||||
import sqlite3
|
||||
import csv
|
||||
from datetime import datetime
|
||||
|
||||
DB = 'nexus.db' # ajusta la ruta si es necesario
|
||||
CSV = 'historial.csv' # ajusta la ruta si es necesario
|
||||
|
||||
def convertir_fecha(fecha_str):
|
||||
"""Convierte MM/DD/YY HH:MM:SS a YYYY-MM-DD"""
|
||||
if not fecha_str:
|
||||
return None
|
||||
try:
|
||||
dt = datetime.strptime(fecha_str.strip(), '%m/%d/%y %H:%M:%S')
|
||||
return dt.strftime('%Y-%m-%d')
|
||||
except:
|
||||
return fecha_str.strip()
|
||||
|
||||
db = sqlite3.connect(DB)
|
||||
|
||||
insertados = 0
|
||||
errores = 0
|
||||
|
||||
with open(CSV, newline='', encoding='utf-8-sig') as f:
|
||||
reader = csv.DictReader(f)
|
||||
for row in reader:
|
||||
matricula = (row.get('Matricula') or '').strip()
|
||||
unidad = (row.get('Unidad') or '').strip()
|
||||
fecha = convertir_fecha(row.get('Cuando', ''))
|
||||
observaciones = (row.get('Observaciones') or '').strip()
|
||||
|
||||
if not matricula:
|
||||
continue
|
||||
|
||||
try:
|
||||
db.execute('''
|
||||
INSERT INTO historial (vehiculo_id, matricula, unidad, fecha, observaciones)
|
||||
VALUES (NULL, ?, ?, ?, ?)
|
||||
''', (matricula, unidad, fecha, observaciones))
|
||||
insertados += 1
|
||||
except Exception as e:
|
||||
print(f'Error en fila {matricula}: {e}')
|
||||
errores += 1
|
||||
|
||||
db.commit()
|
||||
db.close()
|
||||
|
||||
print(f'Importación completada: {insertados} registros insertados, {errores} errores.')
|
||||
Reference in New Issue
Block a user