Ficheros JSON

De WikiEducator
Saltar a: navegación, buscar


Doc

Ejemplo: Datos del tráfico de http://datos.zaragoza.es

from urllib2 import urlopen
from json import load
 
urlt = 'http://www.zaragoza.es/trafico/estado/tiempos2.json'
tiempos = load(urlopen(urlt))
 
# los datos están en la clave tiempos
tiempos = tiempos.get('tiempos')
 
# mostramos inicio y fin de cada tramo
for t in tiempos: 
    print t.get('inicio'), '--->', t.get('fin')
 
# cuánto va a costar ir de la cª madrid al actur?
for t in tiempos:
    ini = t.get('inicio')
    fin = t.get('fin')
    if 'madrid' in ini.lower() and 'actur' in fin.lower():
         print ini, fin, t.get('minutosActual')
 
# Tramos que acumulan retrasos
for t in tiempos:
    act = t.get('minutosActual')
    normal = t.get('minutosNormal')
    if act > normal:
         print t.get('inicio'), t.get('fin')