I just started using Python as hobby, so forgive me if this is a dumb question.
I recently installed a power meter on my computer, and I’m using it to track its power consumption. It gives me real-time minute readings exported to a csv file.
I have parsed and read the file, and now I have a dictionary where the key is a timestamp, stored as a struct_time from the time library. Currently, the dictionary has readings which were taken every minute. I’d like to create a new dictionary whose keys are hour timestamps, and whose values are the sum of the individual minute readings.
I thought about looping through the dictionary with a counter mod 60, but I’m told that isn’t very Pythonic. Also, there’s no guarantee that there exists 60 readings for every hour. How should I do this?
First, I’d suggest using the
datetime.datetimeclass instead oftime.time_struct, since the latter is really just a weak wrapper around a 9-tuple but the former is a fully-featured class with operator overloads etc. Next, you can use acollections.defaultdictto easily construct your desired total readings:If
power_readingis a tuple, you can’t use+=(since that concatenates the tuples instead of doing an element-wise addition), so you’ll have to do something like this instead: