I have the following list:
DATA
data_set = [(2, 2021, '2300'),(1, 2021, '2500'),(12, 2020, '2400'),(11, 2020, '1500')]
I want to format this list into the following one,
EXPECTED OUTPUT
[
{
"2021-01-01T00": [
{
"2021-02-01T00:00:00": 2300
},
{
"2021-01-01T00:00:00": 2500
}
]
},
{
"2020-01-01T00": [
{
"2020-12-01T00:00:00": 2400
},
{
"2020-11-01T00:00:00": 1500
}
]
}
]
I don't know how to format these two years in two objects. I tried hard but as I'm new to python so please help if you know the way to format this data.
EDITED
SAMPLE CODE
result = []
for item in data_set:
result.append({
str(datetime.datetime(item[1], item[0], 1)): float(item[2])
})
With above code I got the following output,
[
{
"2021-02-01 00:00:00": "2300"
},
{
"2021-01-01 00:00:00": "2500"
},
{
"2020-12-01 00:00:00": "2400"
},
{
"2020-11-01 00:00:00": "1500"
}
]
But I want the records of each year separate as I described above in the EXPECTED OUTPUT.
01T00:00:00hardcoded value into your strings?data_setcontains (month,year,value) like (2,2021,'2300'). We can get date value bydatetime.datetime(2021, 2, 1)