Python Forum
from Json Time Serie file to python dictionnary - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: from Json Time Serie file to python dictionnary (/thread-21026.html)



from Json Time Serie file to python dictionnary - Reims - Sep-11-2019

Hi, i have a preoccupation, i know the process to follow but not exactly how to implement it.

so i have a json file which looks like

json = [{"signal_name":"X", "signal_value":"valueX1", "time"="timeX1","par"="id1"}, 

{"signal_name":"Y", "signal_value":"valueY1", "time":"timeY1","par":"id2"},

{"signal_name":"Z", "signal_value":"valueZ1", "time":"timeZ1","par":"id3"},

{"signal_name":"X", "signal_value":"valueX2", "time":"timeX2","par":"id5"}]
and so many other lines...

so the first thing i want to do is store as array the signal name with their different values, and the corresponding time value as follow
Array_nameX = [ "valueX1", "valueX2" … valueXN; "timeX1", "timeX2", … "timeXN"]  

Array_nameY = [ "valueY1", "valueY2" … valueYN; "timeY1", "timeY2", … "timeYN"] 

Array_nameZ = [ "valueZ1", "valueZ2" … valueZN; "timeZ1", "timeZ2", … "timeZN"] 
and finally construct an dictionnary with the key value the name of my signal, and the value as the different arrays

 dict = [X: array_nameX, Y:array_nameY,...]



RE: from Json Time Serie file to python dictionnary - DeaD_EyE - Sep-11-2019

A defaultdict can solve the task.

from collections import defaultdict


data = [{"signal_name":"X", "signal_value":"valueX1", "time":"timeX1","par":"id1"}, 
{"signal_name":"Y", "signal_value":"valueY1", "time":"timeY1","par":"id2"},
{"signal_name":"Z", "signal_value":"valueZ1", "time":"timeZ1","par":"id3"},
{"signal_name":"X", "signal_value":"valueX2", "time":"timeX2","par":"id5"}]

measurements = defaultdict(list)
for signal in data:
    name, value = signal['signal_name'], signal['signal_value']
    measurements[name].append(value)
PS: The original data in your post has a syntax error. You're using a = two times instead of a colon.
It's also not json, it's a Python dict. Json was it before in form of a string.