Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8051

Python • Re: Managing (very) complex Dictionary structures.

$
0
0
i would pretty print this dictionary to a file for clarity.
It might well have helped potential responders to understand the structure better(!), but I was able to get help on the python.org board to which D#256 gave a link.

With very little toing and froing, we came up with the following code, which returns the total energy recorded by a specified 'meter' over a specified time period within that represented by the returned data. You pass the entire directory as the 'data' argument.

Code:

def summarize(data, meter_type, from_date, to_date):# from_date and to_date should be in the same text format as is used in the data - i.e. YYYY-MM-DD HH:MM:SS    total = 0    for types in data["energyDetails"]["meters"]:        if types["type"] == meter_type:            for values in types["values"]:            if 'value' in values:            if (values["date"] >= from_date) and (values["date"] < to_date):            total += values["value"]    return total

Statistics: Posted by pfletch101 — Wed Jan 15, 2025 10:31 pm



Viewing all articles
Browse latest Browse all 8051

Trending Articles