0

I have a data that contains temperature values after every 15 minutes(4 values for each hour). I wanted to create json that stores 96 arrays(4*24) of temperature data for each day. This is my data

22-10-2018,01:00:00,7.6,Station1
22-10-2018,01:15:00,9.0,Station1
22-10-2018,01:30:00,6.3,Station1
22-10-2018,01:45:00,4.1,Station1
22-10-2018,02:00:00,4.5,Station1
22-10-2018,02:15:00,7.3,Station1
22-10-2018,02:30:00,6.1,Station1
..
..
..
23-10-2018,01:30:00,6.3,Station2
23-10-2018,01:45:00,4.1,Station2
23-10-2018,02:00:00,4.5,Station2
23-10-2018,02:15:00,7.3,Station2
23-10-2018,02:30:00,6.1,Station2

I wanted my json to be like

{ Station: Station1,
  Date: 22-10-2018,
  Time:[01:00:00:{temperature:7.6},01:15:00:{temperature:9.0}...]
},
{ Station: Station2,
  Date: 23-10-2018,
  Time:[01:30:00:{temperature:6.3},01:35:00:{temperature:4.0}...]
}..

I am totally new to python and don't know how to iterate between dates and minutes. Any help is highly appreciated.

  • Wouldn't time start at 00:00:00? And if you know you're getting 96 reaadings, why not just have the keys be 'Station', 'Date', and 'Temperature' with a list of 96 items? – Tim Roberts Apr 19 '21 at 00:52
  • 1
    consider using a standard date format in the `json` output - see e.g. https://stackoverflow.com/q/10286204/10197418 – FObersteiner Apr 19 '21 at 07:04

1 Answers1

1

You don't need to parse the dates and times at all. They're just strings. Consider this solution:

import json 

data = """\
22-10-2018,01:00:00,7.6,Station1
22-10-2018,01:15:00,9.0,Station1
22-10-2018,01:30:00,6.3,Station1
22-10-2018,01:45:00,4.1,Station1
22-10-2018,02:00:00,4.5,Station1
22-10-2018,02:15:00,7.3,Station1
22-10-2018,02:30:00,6.1,Station1
23-10-2018,01:30:00,6.3,Station2
23-10-2018,01:45:00,4.1,Station2
23-10-2018,02:00:00,4.5,Station2
23-10-2018,02:15:00,7.3,Station2
23-10-2018,02:30:00,6.1,Station2""".splitlines()

outdata = []
anydata = {}
lastkey = None
for line in data:
    date,time,temp,stn = line.split(',')
    if (stn,date) != lastkey:
        if anydata:
            outdata.append(anydata)
        lastkey = stn,date
        anydata = { "Station": stn, "Date": date, "Temps": [] }
    anydata["Temps"].append( (time,float(temp)) )
outdata.append(anydata)

print(json.dumps(outdata))

This produces one element per station/date combo, which has a list of time/temperature combinations:

timr@tims-gram:~/src$ python x.py | jq .
[
  {
    "Station": "Station1",
    "Date": "22-10-2018",
    "Temps": [
      [
        "01:00:00",
        7.6
      ],
      [
        "01:15:00",
        9
      ],
      [
        "01:30:00",
        6.3
      ],
      [
        "01:45:00",
        4.1
      ],
      [
        "02:00:00",
        4.5
      ],
      [
        "02:15:00",
        7.3
      ],
      [
        "02:30:00",
        6.1
      ]
    ]
  },
  {
    "Station": "Station2",
    "Date": "23-10-2018",
    "Temps": [
      [
        "01:30:00",
        6.3
      ],
      [
        "01:45:00",
        4.1
      ],
      [
        "02:00:00",
        4.5
      ],
      [
        "02:15:00",
        7.3
      ],
      [
        "02:30:00",
        6.1
      ]
    ]
  }
]
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30