Hello: making progress but still struggling. I have the following json:
json =
{
"emeter": {
"get_daystat": {
"day_list": [
{ "year": 2016, "month": 10, "day": 1, "energy": 0.651000 },
{ "year": 2016, "month": 10, "day": 2, "energy": 0.349000 },
{ "year": 2016, "month": 10, "day": 3, "energy": 0.481000 }
],
"err_code": 0
}
}
}
I am using a linear search to find the energy value from a specific day with this function:
parsed_json = json.loads(json)
def get_energy_value_by_date(obj, year, month, day):
for value in obj['emeter']['get_daystat']['day_list']:
if value['year'] == year and value['month'] == month and value['day'] == day:
return value['energy']
energy = get_energy_value_by_date(parsed_json, 2016, 10, 2)
So far so good. What I need to do next is find the energy value for various days. For example today (assume json is valid):
import datetime
day_now = datetime.datetime.now().strftime("%d")
month_now = datetime.datetime.now().strftime("%m")
year_now = datetime.datetime.now().strftime("%Y")
parsed_json = json.loads(json)
def get_energy_value_by_date(obj, year, month, day):
for value in obj['emeter']['get_daystat']['day_list']:
if value['year'] == year and value['month'] == month and value['day'] == day:
return value['energy']
energy_today = get_energy_value_by_date(parsed_json, year_now, month_now, day_now)
print energy_today
When I run this script it returns
None
I must be missing something basic here. What I need is the ability to pull the energy value for any day of any month of any year for further processing.
Thanks!
Baobab