(免责声明:我是python的新手,这是我关于stackoverflow的第一个问题,请耐心等待我!)
背景:
我正在尝试自动完成一些日常任务,并选择为此目的学习python,
其中一项任务是连接到api并将每小时收入拉到日期“x”
我当前的'程序'成功连接并检索Input()日期
的数据import requests
import json
from datetime import datetime, timedelta
autodatemonth = datetime.strftime(datetime.now() + timedelta(1), '%Y-%m')
enterday = input('Enter day: \n')
AutoStartDate = str(autodatemonth) + '-' + str(enterday)
MonthPlusOne = int(enterday) + 1
AutoEndDate = str(autodatemonth) + '-' + str(MonthPlusOne)
parameters = {'start_date': AutoStartDate, 'end_date': AutoEndDate, 'fields': ['date', 'revenue'], 'start_at_row': 1, 'api_key': ‘xxx’, 'id': xxx, 'row_limit':24}
# Make a get request with the parameters.
response = requests.get("https://example.com/api/Reports/HourlySummary", params=parameters)
lst = response.json()
import pprint
pprint.pprint(lst)
# getting closer
for info in lst['data']:
print(info['revenue'])
上面的代码吐出了这样的回复:
{'data': [{'date': '2018-05-15T02:00:00', 'revenue': 0.0},
{'date': '2018-05-15T03:00:00', 'revenue': 0.0},
{'date': '2018-05-15T04:00:00', 'revenue': 0.0},
{'date': '2018-05-15T05:00:00', 'revenue': 0.0},
{'date': '2018-05-15T06:00:00', 'revenue': 20.0},
{'date': '2018-05-15T07:00:00', 'revenue': 0.0},
{'date': '2018-05-15T08:00:00', 'revenue': 0.0},
{'date': '2018-05-15T09:00:00', 'revenue': 0.0},
{'date': '2018-05-15T10:00:00', 'revenue': 38.0},
{'date': '2018-05-15T11:00:00', 'revenue': 0.0},
{'date': '2018-05-15T12:00:00', 'revenue': 0.0},
{'date': '2018-05-15T13:00:00', 'revenue': 93.0},
{'date': '2018-05-15T14:00:00', 'revenue': 0.0},
{'date': '2018-05-15T15:00:00', 'revenue': 0.0},
{'date': '2018-05-15T16:00:00', 'revenue': 75.0},
{'date': '2018-05-15T17:00:00', 'revenue': 27.0},
{'date': '2018-05-15T18:00:00', 'revenue': 91.0},
{'date': '2018-05-15T19:00:00', 'revenue': 75.0},
{'date': '2018-05-15T20:00:00', 'revenue': 75.0},
{'date': '2018-05-15T21:00:00', 'revenue': 0.0},
{'date': '2018-05-15T22:00:00', 'revenue': 0.0},
{'date': '2018-05-15T23:00:00', 'revenue': 38.0}],
'message': None,
'row_count': 24,
'success': True}
0.0
0.0
0.0
0.0
20.0
0.0
0.0
0.0
38.0
0.0
0.0
93.0
0.0
0.0
75.0
27.0
91.0
75.0
75.0
0.0
0.0
38.0
我试图弄清楚如何从上面的回复中添加收益浮动值。
查看了多个问题和答案,但仍然试图将事情放在一起以适应我自己的情况。
你能指出我正确的方向吗?
编辑: 我只想要总收入
答案 0 :(得分:0)
将列表理解与sum()
一起使用。
<强>实施例强>
lst = {'data': [{'date': '2018-05-15T02:00:00', 'revenue': 0.0},
{'date': '2018-05-15T03:00:00', 'revenue': 0.0},
{'date': '2018-05-15T04:00:00', 'revenue': 0.0},
{'date': '2018-05-15T05:00:00', 'revenue': 0.0},
{'date': '2018-05-15T06:00:00', 'revenue': 20.0},
{'date': '2018-05-15T07:00:00', 'revenue': 0.0},
{'date': '2018-05-15T08:00:00', 'revenue': 0.0},
{'date': '2018-05-15T09:00:00', 'revenue': 0.0},
{'date': '2018-05-15T10:00:00', 'revenue': 38.0},
{'date': '2018-05-15T11:00:00', 'revenue': 0.0},
{'date': '2018-05-15T12:00:00', 'revenue': 0.0},
{'date': '2018-05-15T13:00:00', 'revenue': 93.0},
{'date': '2018-05-15T14:00:00', 'revenue': 0.0},
{'date': '2018-05-15T15:00:00', 'revenue': 0.0},
{'date': '2018-05-15T16:00:00', 'revenue': 75.0},
{'date': '2018-05-15T17:00:00', 'revenue': 27.0},
{'date': '2018-05-15T18:00:00', 'revenue': 91.0},
{'date': '2018-05-15T19:00:00', 'revenue': 75.0},
{'date': '2018-05-15T20:00:00', 'revenue': 75.0},
{'date': '2018-05-15T21:00:00', 'revenue': 0.0},
{'date': '2018-05-15T22:00:00', 'revenue': 0.0},
{'date': '2018-05-15T23:00:00', 'revenue': 38.0}],
'message': None,
'row_count': 24,
'success': True}
res = sum([i['revenue'] for i in lst["data"]])
print(res)
<强>输出:强>
532.0
答案 1 :(得分:0)
print(sum(info['revenue'] for info in lst['data']))