I have a server that reveives a set of data like this :
['Date(yyyy - mm - dd)', 'Time', 'Conductivity(mS / cm)', 'Temperature(C)', 'Depth(m)', 'Battery(V)', 'Salinity(PSU)', 'Density (kg m-3)', 'Calc, SV (m/s)']
['18/01/2017', '08:04,3', '12,217', '8,701', '2,1', '7,6', '10,453', '1008,002', '1455,04']
['18/01/2017', '08:04,8', '12,256', '8,695', '2,19', '7,6', '10,49', '1008,031', '1455,06']
['18/01/2017', '08:05,3', '36,04', '8,697', '2,17', '7,6', '34,131', '1026,495', '1484,05']
['18/01/2017', '08:05,8', '36,003', '8,689', '2,23', '7,6', '34,099', '1026,472', '1483,98']
['18/01/2017', '08:06,3', '36,088', '8,696', '2,32', '7,6', '34,182', '1026,536', '1484,11']
['18/01/2017', '08:06,8', '35,973', '8,699', '2,44', '7,6', '34,058', '1026,439', '1483,97']
['18/01/2017', '08:07,3', '36,121', '8,716', '2,52', '7,6', '34,198', '1026,546', '1484,2']
The first line are my keys, and what's next are the values. I want to know how to convert this type of list to a json file.
Something like this : { "date": "18/01/2017", "time":"08:04,3".... } Thank you
Try dict()
and zip()
functions:
import json
captions = ['Date(yyyy - mm - dd)', 'Time', 'Conductivity(mS / cm)', 'Temperature(C)',
'Depth(m)', 'Battery(V)', 'Salinity(PSU)', 'Density (kg m-3)', 'Calc, SV (m/s)']
data = [
['18/01/2017', '08:04,3', '12,217', '8,701', '2,1', '7,6', '10,453', '1008,002', '1455,04'],
['18/01/2017', '08:04,8', '12,256', '8,695', '2,19', '7,6', '10,49', '1008,031', '1455,06'],
['18/01/2017', '08:05,3', '36,04', '8,697', '2,17', '7,6', '34,131', '1026,495', '1484,05'],
]
prepared = [dict(zip(captions, row)) for row in data]
j = json.dumps(prepared)
print(j)
Output:
[{"Date(yyyy - mm - dd)": "18/01/2017", "Salinity(PSU)": "10,453", "Temperature(C)": "8,701", "Depth(m)": "2,1", "Conductivity(mS / cm)": "12,217", "Density (kg m-3)": "1008,002", "Battery(V)": "7,6", "Time": "08:04,3", "Calc, SV (m/s)": "1455,04"}, {"Date(yyyy - mm - dd)": "18/01/2017", "Salinity(PSU)": "10,49", "Temperature(C)": "8,695", "Depth(m)": "2,19", "Conductivity(mS / cm)": "12,256", "Density (kg m-3)": "1008,031", "Battery(V)": "7,6", "Time": "08:04,8", "Calc, SV (m/s)": "1455,06"}, {"Date(yyyy - mm - dd)": "18/01/2017", "Salinity(PSU)": "34,131", "Temperature(C)": "8,697", "Depth(m)": "2,17", "Conductivity(mS / cm)": "36,04", "Density (kg m-3)": "1026,495", "Battery(V)": "7,6", "Time": "08:05,3", "Calc, SV (m/s)": "1484,05"}]