Diferencia entre revisiones de «Usuario:Lmorillas/modulo programacion/python/json»

De WikiEducator
Saltar a: navegación, buscar
(Página creada con '{{DISPLAYTITLE:Ficheros JSON}} {{TEP}} == Doc == * http://www.doughellmann.com/PyMOTW/json/ * http://developer.yahoo.com/python/python-json.html')
 
Línea 1: Línea 1:
 
{{DISPLAYTITLE:Ficheros JSON}}
 
{{DISPLAYTITLE:Ficheros JSON}}
  
 
{{TEP}}
 
  
 
== Doc ==
 
== Doc ==
 
* http://www.doughellmann.com/PyMOTW/json/
 
* http://www.doughellmann.com/PyMOTW/json/
 
* http://developer.yahoo.com/python/python-json.html
 
* http://developer.yahoo.com/python/python-json.html
 +
 +
== Ejemplo: Datos del tráfico de http://datos.zaragoza.es==
 +
<source lang="python">
 +
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')
 +
</source>

Revisión de 22:11 30 nov 2011


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')