terminado
This commit is contained in:
@@ -1,60 +1,104 @@
|
||||
from validacion import *
|
||||
from validacion import validarCoordenada, validarInstante, validarTelefono
|
||||
from variables import *
|
||||
from util import decimalGrado, convertirSegundos
|
||||
|
||||
def normalizar_instante(instante, formato):
|
||||
|
||||
def normalizarInstante(instante, formato):
|
||||
d = validarInstante(instante)
|
||||
if d is None:
|
||||
return None
|
||||
|
||||
if formato == 1:
|
||||
if not d['mes'].isdigit():
|
||||
d['mes'] = meses.index(d['mes'].lower()) + 1
|
||||
d = convertirSegundos(d)
|
||||
|
||||
return f"{d['año']}-{d['mes']:0>2}-{d['dia']:0>2} {d['hora']:0>2}:{d['minuto']}"
|
||||
|
||||
elif formato == 2:
|
||||
if d['mes'] in meses:
|
||||
return f"{d['mes']} {d['dia']}, {d['año']} {d['hora']}:{d['minuto']} {d['segundo']}"
|
||||
else:
|
||||
if int(d['hora']) > 12:
|
||||
d['hora'] = int(d['hora']) % 12
|
||||
try:
|
||||
d['mes'] = meses[int(d['mes']) - 1]
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
if d['mes'].isdigit():
|
||||
d['mes'] = meses[int(d['mes']) - 1]
|
||||
if int(d['hora']) >= 12:
|
||||
d['hora'] = int(d['hora']) % 12
|
||||
if d['hora'] == 0:
|
||||
d['hora'] = 12
|
||||
|
||||
return f"{d['mes'].lower()} {d['dia']}, {d['año']} {d['hora']}:{d['minuto']} AM"
|
||||
return f"{d['mes'].lower()} {d['dia']}, {d['año']} {d['hora']}:{d['minuto']} PM"
|
||||
|
||||
if int(d['hora']) == 0:
|
||||
d['hora'] = 12
|
||||
|
||||
return f"{d['mes'].lower()} {d['dia']}, {d['año']} {d['hora']}:{d['minuto']} AM"
|
||||
|
||||
elif formato == 3:
|
||||
if not d['mes'].isdigit():
|
||||
d['mes'] = meses.index(d['mes'].lower()) + 1
|
||||
d = convertirSegundos(d)
|
||||
return f"{d['hora']:0>2}:{d['minuto']}:{d['segundo']:0>2} {d['dia']:0>2}/{d['mes']:0>2}/{d['año']}"
|
||||
|
||||
if not d['segundo'].isdigit():
|
||||
d['segundo'] = '00'
|
||||
return f"{d['hora']:0>2}:{d['minuto']}:{d['segundo']} {d['dia']:0>2}/{d['mes']:0>2}/{d['año']}"
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
def normalizar_telefono(telefono):
|
||||
telefono = re.sub(r'( )|(-)', r'', telefono)
|
||||
if telefono[0] != '+':
|
||||
return f'+34{telefono}'
|
||||
return telefono
|
||||
def normalizarTelefono(telefono):
|
||||
telf = validarTelefono(telefono)
|
||||
if telf is None:
|
||||
return None
|
||||
else:
|
||||
if 'num' in telf:
|
||||
num = re.sub(r'( )|(-)', r'', telf['num'])
|
||||
return f'+{num}'
|
||||
elif 'parte1' in telf:
|
||||
telf = re.sub(r'( )|(-)', r'', telefono)
|
||||
if telf[0] != '+':
|
||||
return f'+34{telf}'
|
||||
return telf
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
def normalizarCoordenada(coordenada, formato):
|
||||
coordenada = validarCoordenada(coordenada)
|
||||
if coordenada is None:
|
||||
return None
|
||||
|
||||
if formato == 1:
|
||||
return f"{coordenada['latitud']}, {coordenada['longitud']}"
|
||||
|
||||
elif formato == 2 or formato == 3:
|
||||
letraLatitud = 'N'
|
||||
letraLongitud = 'E'
|
||||
if coordenada['latitud'] < 0:
|
||||
coordenada['latitud'] *= -1
|
||||
letraLatitud = 'S'
|
||||
|
||||
if coordenada['longitud'] < 0:
|
||||
coordenada['longitud'] *= -1
|
||||
letraLongitud = 'W'
|
||||
|
||||
gradosLatitud, minutosLatitud, segundosLatitud = decimalGrado(coordenada['latitud'])
|
||||
gradosLongitud, minutosLongitud, segundosLongitud = decimalGrado(coordenada['longitud'])
|
||||
|
||||
if formato == 2:
|
||||
return f'{gradosLatitud}º {minutosLatitud}\' {segundosLatitud:}.0000" {letraLatitud}, {gradosLongitud}º {minutosLongitud}\' {segundosLongitud}.0000" {letraLongitud}'
|
||||
|
||||
else:
|
||||
return f'{gradosLatitud:0>3}{minutosLatitud:0>2}{segundosLatitud:0>2}.0000{letraLatitud}{gradosLongitud:0>3}{minutosLongitud:0>2}{segundosLongitud:0>2}.0000{letraLongitud}'
|
||||
|
||||
else:
|
||||
return None
|
||||
|
||||
def normalizar_coordenada(coordenada, formato):
|
||||
c = validarCoordenada(coordenada, formato)
|
||||
return c
|
||||
|
||||
def normalizar(fichero, formato_fecha, formato_coordenadas):
|
||||
try:
|
||||
f = open(fichero, 'r', encoding='utf-8')
|
||||
except:
|
||||
exit(1)
|
||||
n = 0
|
||||
for i in f.readlines():
|
||||
telefono, nif, instante, coordenada, producto, precio = i.split(";")
|
||||
instante = normalizar_instante(instante.strip(), formato_fecha)
|
||||
telefono = normalizar_telefono(telefono.strip())
|
||||
n_coordenada = normalizar_coordenada(coordenada.strip(), formato_coordenadas)
|
||||
if n_coordenada is None:
|
||||
continue
|
||||
n += 1
|
||||
|
||||
print(n)
|
||||
for linea in f.readlines():
|
||||
telefono, nif, instante, coordenada, producto, precio = linea.split(";")
|
||||
instante = normalizarInstante(instante.strip(), formato_fecha)
|
||||
telefono = normalizarTelefono(telefono.strip())
|
||||
coordenada = normalizarCoordenada(coordenada.strip(), formato_coordenadas)
|
||||
if coordenada is None:
|
||||
continue
|
||||
print(f"{telefono} ; {nif} ; {instante} ; {coordenada} ; {producto} ; {precio}", end='')
|
||||
Reference in New Issue
Block a user