[Python-es] Expresiones regulares

Flavio Danesse fdanesse en gmail.com
Mar Jun 3 23:03:46 CEST 2014


ok, dame unos minutos, me pongo en esto y te aviso.


El 3 de junio de 2014, 17:55, Miguel Angel Rodriguez <marodal en gmail.com>
escribió:

> Este es un ejemplo de lo que recibo:
>
> {"format":"JSON","result":{"devices":[{"key":"SCGDE1BA:180340320","channels":[{"name":"PF","unit":"","meta":"PF","value":"1.000"},{"name":"P-WSpt","unit":"kW","meta":"P-WSpt","value":"935"},
>
> {"name":"Vac","unit":"V","meta":"Vac","value":"217.5"},{"name":"Iac","unit":"A","meta":"Iac","value":"513.8"},{"name":"Pac","unit":"kW","meta":"Pac","value":"335.2"},{"name":"Qac","unit":"kV
>
> Ar","meta":"Qac","value":"-1.0"},{"name":"Sac","unit":"kVA","meta":"Sac","value":"335.1"},{"name":"Fac","unit":"Hz","meta":"Fac","value":"49.950"},{"name":"ErrNoFirst","unit":"","meta":"ErrNoFirst","value":"0"},{"
>
> name":"P-WModFailStt","unit":"","meta":"P-WModFailStt","value":"Off"},{"name":"P-WModStt","unit":"","meta":"P-WModStt","value":"Off"},{"name":"Dsc","unit":"","meta":"Dsc","value":"---"},{"name":"Firmware-9","unit":"","meta":"Firmware-9","value":"
> "},{"name":"Cntry","unit":"","meta":"Cntry","value":"DE1"}
> ,{"name":"Type","unit":"","meta":"Type","value":"SCG"},{"name":"Mode","unit":"","meta":"Mode","value":"Operation"},{"name":"Error","unit":"","meta":"Error","value":"---"}]}]},"version":"1.0"
>
>
>
> Quiero parsearlo para poder almacenar en unas variables que tengo los
> values que aparecen en el JSON. De esta forma:
>
>  Con la solucion con expresiones regulares en cada iteracion tengo lo
> siguiente:
>  d_value = [name,(valor del name),unit,(valor del unit),meta,(valor del
> meta),value,(valor del value)]
>
> En esa iteracion hago lo siguiente:
> if d_value[1] == "PF":
>     mi_variable_PF = d_value[7]
> if d_value[1] == "Fac":
>    mi_variable_Fac = d_value[7]
> .....
>
> No necesito todos los values, solo los que corresponden a unos
> determinados name.
>
> Con la solucion con expresiones regulares tengo el siguiente bug:
> Alguna veces en el json aparece  "unit":"". Es decir, la clave key no
> tiene valor, por lo que obtengo un array de solo 7 elementos (sin el
> elemento valor del unit) y consigo recuperar el value para ese name.
> ¿Alguien me podría ayudar con esto?
>
> Por ultimo, con la solucion con JSON, ¿es necesario usar ficheros? No se
> como será el manejo de ficheros en el sistema donde va a funcionar este
> python.
>
>
> Gracias y un saludo
>
>
> El 3 de junio de 2014, 22:53, Miguel Angel Rodriguez <marodal en gmail.com>
> escribió:
>
> Este es un ejemplo de lo que recibo:
>>
>> {"format":"JSON","result":{"devices":[{"key":"SCGDE1BA:
>> 180340320","channels":[{"name":"PF","unit":"","meta":"PF","
>> value":"1.000"},{"name":"P-WSpt","unit":"kW","meta":"P-WSpt","value":"935"},
>>       {"name":"Vac","unit":"V","meta":"Vac","value":"217.5"},{
>> "name":"Iac","unit":"A","meta":"Iac","value":"513.8"},{"
>> name":"Pac","unit":"kW","meta":"Pac","value":"335.2"},{"name":"Qac","unit":"kV
>>       Ar","meta":"Qac","value":"-1.0"},{"name":"Sac","unit":"kVA"
>> ,"meta":"Sac","value":"335.1"},{"name":"Fac","unit":"Hz","
>> meta":"Fac","value":"49.950"},{"name":"ErrNoFirst","unit":""
>> ,"meta":"ErrNoFirst","value":"0"},{"       name":"P-WModFailStt","unit":"
>> ","meta":"P-WModFailStt","value":"Off"},{"name":"P-
>> WModStt","unit":"","meta":"P-WModStt","value":"Off"},{"
>> name":"Dsc","unit":"","meta":"Dsc","value":"---"},{"name":"
>> Firmware-9","unit":"","meta":"Firmware-9","value":"
>> "},{"name":"Cntry","unit":"","meta":"Cntry","value":"DE1"}
>> ,{"name":"Type","unit":"","meta":"Type","value":"SCG"},{"
>> name":"Mode","unit":"","meta":"Mode","value":"Operation"},{"
>> name":"Error","unit":"","meta":"Error","value":"---"}]}]},"version":"1.0"
>>
>>
>>
>> Quiero parsearlo para poder almacenar en unas variables que tengo los
>> values que aparecen en el JSON. De esta forma:
>>
>>  Con la solucion con expresiones regulares en cada iteracion tengo lo
>> siguiente:
>>  d_value = [name,(valor del name),unit,(valor del unit),meta,(valor del
>> meta),value,(valor del value)]
>>
>> En esa iteracion hago lo siguiente:
>> if d_value[1] == "PF":
>>     mi_variable_PF = d_value[7]
>> if d_value[1] == "Fac":
>>    mi_variable_Fac = d_value[7]
>> .....
>>
>> No necesito todos los values, solo los que corresponden a unos
>> determinados name.
>>
>> Con la solucion con expresiones regulares tengo el siguiente bug:
>> Alguna veces en el json aparece  "unit":"". Es decir, la clave key no
>> tiene valor, por lo que no consigo
>>
>>
>> El 3 de junio de 2014, 22:40, Flavio Danesse <fdanesse en gmail.com>
>> escribió:
>>
>> Si quieres pasame un string de ejemplo que esté completo y dime que hacer
>>> con los values y te escribo la solución.
>>>
>>>
>>> El 3 de junio de 2014, 17:38, Flavio Danesse <fdanesse en gmail.com>
>>> escribió:
>>>
>>> Lo que hago es tomar el string que tienes y guardarlo en un archivo como
>>>> texto plano pero luego lo cargo utilizando json con lo cual obtengo un
>>>> diccionario python. Para obtener las keys y values del diccionario solo
>>>> tienes que hacer diccionario.keys() y diccionario.values() o puedes hacer
>>>> también diccionario.items() lo cual te da la pareja key:value.
>>>>
>>>> En todos los casos obtendrás lista de keys, values o items.
>>>>
>>>> Ahora bien, el primer obstáculo está salvado, que era pasar el string a
>>>> un diccionario donde tienes parejas key-value.
>>>>
>>>> El segundo obstaculo es hacer recursividad dentro del diccionario para
>>>> seguir hacia adentro de la estructura obteniendo los diccionarios internos.
>>>> No es tan complicado, puedes comenzar con algo como:
>>>>
>>>> for key in diccionario.keys():
>>>>     valor = diccionario[key]
>>>>     . . .  lo que quieras con el key y lo que quieras con value aca
>>>> importa que tipo de dato es el value, porque por lo que veo tienes listas,
>>>> diccionarios, string, etc, utiliza type(valor) para operar sobre ellos
>>>> según su tipo.
>>>>
>>>>
>>>>
>>>>
>>>> El 3 de junio de 2014, 17:13, Miguel Angel Rodriguez <marodal en gmail.com
>>>> > escribió:
>>>>
>>>> No entiendo que haces con archivo...
>>>>>
>>>>> Nunca he programado con json en python.
>>>>>
>>>>> ¿Como seria a partir de lo que indicas el parseo de los datos? Es
>>>>> decir, ¿Como seria el bucle que me iria dando el name y el value?
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> 2014-06-03 22:08 GMT+02:00 Flavio Danesse <fdanesse en gmail.com>:
>>>>>
>>>>> Voto por lo correcto que sería así:
>>>>>>
>>>>>> import os
>>>>>> import json
>>>>>> import codecs
>>>>>>
>>>>>> text =
>>>>>> '{"format":"JSON","result":{"devices":[{"key":"SCGDE1BA:180340320", etc
>>>>>> . . .
>>>>>>
>>>>>> path = "/tmp/x.json"
>>>>>>
>>>>>> archivo = open(path, "w")
>>>>>> archivo.write(text)
>>>>>> archivo.close()
>>>>>>
>>>>>> archivo = codecs.open(path, "r", "utf-8")
>>>>>> diccionario = json.JSONDecoder("utf-8").decode(archivo.read())
>>>>>>
>>>>>>
>>>>>> El 3 de junio de 2014, 8:03, <gustavo en nodo50.org> escribió:
>>>>>>
>>>>>> ejecutando esto:
>>>>>>>
>>>>>>> #!/usr/bin/python3
>>>>>>> import re
>>>>>>> value_str =
>>>>>>>
>>>>>>> '{"format":"JSON","result":{"devices":[{"key":"SCGDE1BA:180340320","channels":[{"name":"PF","unit":"","meta":"PF","value":"1.000"},{"name":"P-WSpt","unit":"kW","meta":"P-WSpt","value":"935"},{"name":"Vac","unit":"V","meta":"Vac","value":"217.5"},{"name":"Iac","unit":"A","meta":"Iac","value":"513.8"},{"name":"Pac","unit":"kW","meta":"Pac","value":"335.2"},{"name":"Qac","unit":"kVAr","meta":"Qac","value":"-1.0"},{"name":"Sac","unit":"kVA","meta":"Sac","value":"335.1"},{"name":"Fac","unit":"Hz","meta":"Fac","value":"49.950"},{"name":"ErrNoFirst","unit":"","meta":"ErrNoFirst","value":"0"},{"name":"P-WModFailStt","unit":"","meta":"P-WModFailStt","value":"Off"},{"name":"P-WModStt","unit":"","meta":"P-WModStt","value":"Off"},{"name":"Dsc","unit":"","meta":"Dsc","value":"---"},{"name":"Firmware-9","unit":"","meta":"Firmware-9","value":""},{"name":"Cntry","unit":"","meta":"Cntry","value":"DE1"}
>>>>>>>
>>>>>>> ,{"name":"Type","unit":"","meta":"Type","value":"SCG"},{"name":"Mode","unit":"","meta":"Mode","value":"Operation"},{"name":"Error","unit":"","meta":"Error","value":"---"}]}]},"version":"1.0"'
>>>>>>>
>>>>>>> for channel_str in
>>>>>>> re.findall("\"name\":\"[A-Za-z0-9\-.]+\"",value_str):
>>>>>>>  for channel_str1 in
>>>>>>> re.findall("\"value\":\"[A-Za-z0-9\-.]+\"",value_str):
>>>>>>>   print ( channel_str + ";" + channel_str1 )
>>>>>>>
>>>>>>> me da esto:
>>>>>>>
>>>>>>> "name":"Cntry";"value":"335.2"
>>>>>>> "name":"Cntry";"value":"-1.0"
>>>>>>> "name":"Cntry";"value":"335.1"
>>>>>>> "name":"Cntry";"value":"49.950"
>>>>>>> "name":"Cntry";"value":"0"
>>>>>>> "name":"Cntry";"value":"Off"
>>>>>>> "name":"Cntry";"value":"Off"
>>>>>>> "name":"Cntry";"value":"---"
>>>>>>> "name":"Cntry";"value":"DE1"
>>>>>>> "name":"Cntry";"value":"SCG"
>>>>>>> "name":"Cntry";"value":"Operation"
>>>>>>> "name":"Cntry";"value":"---"
>>>>>>> "name":"Type";"value":"1.000"
>>>>>>> "name":"Type";"value":"935"
>>>>>>> "name":"Type";"value":"217.5"
>>>>>>> "name":"Type";"value":"513.8"
>>>>>>> "name":"Type";"value":"335.2"
>>>>>>> "name":"Type";"value":"-1.0"
>>>>>>> "name":"Type";"value":"335.1"
>>>>>>> "name":"Type";"value":"49.950"
>>>>>>>
>>>>>>>
>>>>>>> te vale?
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> > Hola a todos,
>>>>>>> >
>>>>>>> > Estoy intentando parsear con Python una gran cadena que recibo en
>>>>>>> un
>>>>>>> > software de comunicaciones que estoy programando.
>>>>>>> >
>>>>>>> > La cadena que yo recibo es la siguiente:
>>>>>>> >
>>>>>>> > value_str =
>>>>>>> >
>>>>>>> {"format":"JSON","result":{"devices":[{"key":"SCGDE1BA:180340320","channels":[{"name":"PF","unit":"","meta":"PF","value":"1.000"},{"name":"P-WSpt","unit":"kW","meta":"P-WSpt","value":"935"},
>>>>>>> >
>>>>>>> >
>>>>>>> {"name":"Vac","unit":"V","meta":"Vac","value":"217.5"},{"name":"Iac","unit":"A","meta":"Iac","value":"513.8"},{"name":"Pac","unit":"kW","meta":"Pac","value":"335.2"},{"name":"Qac","unit":"kV
>>>>>>> >
>>>>>>> >
>>>>>>> Ar","meta":"Qac","value":"-1.0"},{"name":"Sac","unit":"kVA","meta":"Sac","value":"335.1"},{"name":"Fac","unit":"Hz","meta":"Fac","value":"49.950"},{"name":"ErrNoFirst","unit":"","meta":"ErrNoFirst","value":"0"},{"
>>>>>>> >
>>>>>>> >
>>>>>>> name":"P-WModFailStt","unit":"","meta":"P-WModFailStt","value":"Off"},{"name":"P-WModStt","unit":"","meta":"P-WModStt","value":"Off"},{"name":"Dsc","unit":"","meta":"Dsc","value":"---"},{"name":"Firmware-9","unit":"","meta":"Firmware-9","value":"
>>>>>>> > "},{"name":"Cntry","unit":"","meta":"Cntry","value":"DE1"}
>>>>>>> >
>>>>>>> ,{"name":"Type","unit":"","meta":"Type","value":"SCG"},{"name":"Mode","unit":"","meta":"Mode","value":"Operation"},{"name":"Error","unit":"","meta":"Error","value":"---"}]}]},"version":"1.0"
>>>>>>> >
>>>>>>> >
>>>>>>> > Yo solo quiero quedarme con los name y value existente.
>>>>>>> >
>>>>>>> > LA forma en la que lo estoy intentando es la siguiente:
>>>>>>> >
>>>>>>> > for channel_str in re.findall("\"name\":\"[A-Za-z0-9\-.
>>>>>>> > ]+\",\"value\":\"[A-Za-z0-9\-. ]*\"",value_str):
>>>>>>> >             d_value = re.findall("[A-Za-z0-9.\-]+",channel_str)
>>>>>>> >
>>>>>>> > Pero de esta forma no entra en el for.
>>>>>>> >
>>>>>>> > ¿Alguien me puede ayudar?
>>>>>>> >
>>>>>>> > Gracias y un saludo
>>>>>>> >
>>>>>>> >
>>>>>>> >
>>>>>>> >
>>>>>>> >
>>>>>>> >
>>>>>>> > --
>>>>>>> > Miguel Ángel Rodríguez de Alba
>>>>>>> > 678304435
>>>>>>> > marodal en gmail.com
>>>>>>> > _______________________________________________
>>>>>>> > Python-es mailing list
>>>>>>> > Python-es en python.org
>>>>>>> > https://mail.python.org/mailman/listinfo/python-es
>>>>>>> > FAQ: http://python-es-faq.wikidot.com/
>>>>>>> >
>>>>>>>
>>>>>>>
>>>>>>> _______________________________________________
>>>>>>> Python-es mailing list
>>>>>>> Python-es en python.org
>>>>>>> https://mail.python.org/mailman/listinfo/python-es
>>>>>>> FAQ: http://python-es-faq.wikidot.com/
>>>>>>>
>>>>>>
>>>>>>
>>>>>> _______________________________________________
>>>>>> Python-es mailing list
>>>>>> Python-es en python.org
>>>>>> https://mail.python.org/mailman/listinfo/python-es
>>>>>> FAQ: http://python-es-faq.wikidot.com/
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> Miguel Ángel Rodríguez de Alba
>>>>> 678304435
>>>>> marodal en gmail.com
>>>>>
>>>>> _______________________________________________
>>>>> Python-es mailing list
>>>>> Python-es en python.org
>>>>> https://mail.python.org/mailman/listinfo/python-es
>>>>> FAQ: http://python-es-faq.wikidot.com/
>>>>>
>>>>>
>>>>
>>>
>>> _______________________________________________
>>> Python-es mailing list
>>> Python-es en python.org
>>> https://mail.python.org/mailman/listinfo/python-es
>>> FAQ: http://python-es-faq.wikidot.com/
>>>
>>>
>>
>>
>> --
>> Miguel Ángel Rodríguez de Alba
>> 678304435
>> marodal en gmail.com
>>
>
>
>
> --
> Miguel Ángel Rodríguez de Alba
> 678304435
> marodal en gmail.com
>
> _______________________________________________
> Python-es mailing list
> Python-es en python.org
> https://mail.python.org/mailman/listinfo/python-es
> FAQ: http://python-es-faq.wikidot.com/
>
>
------------ próxima parte ------------
Se ha borrado un adjunto en formato HTML...
URL: <http://mail.python.org/pipermail/python-es/attachments/20140603/53aa8c89/attachment.html>


Más información sobre la lista de distribución Python-es