简体   繁体   中英

Python: How to iterate between temperature values of each day to create json temperature array for each day?

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.

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
      ]
    ]
  }
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM