我在这个api中有一个动态的行范围。我已经看过了一些先前使用的while循环,但我似乎无法弄清楚如何在这个脚本中交换for循环for while循环。
header = ["fanduel", "fd_first_name", "fd_last_name", "fd_player_salary", "draftkings", "dk_first_name", "dk_last_name", "dk_player_salary"]
headers = {
"Authorization": "Basic " +
base64.b64encode('{}:{}'.format(USERNAME,PASSWORD)\
.encode('utf-8')).decode('ascii')
}
with open("dailydfs.csv", 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(header)
for fordate in range(20171215,20171216): #ENTER DATE'S
req = requests.get(url="https://api.mysportsfeeds.com/v1.1/pull/nhl/2017-2018-regular/daily_dfs.json?fordate=" + str(fordate) , headers=headers)
req.raise_for_status()
data = req.json()
daily_dfs = data['dailydfs']
fanduel = daily_dfs['dfsEntries'][0]['dfsType']
draftkings = daily_dfs['dfsEntries'][1]['dfsType']
for i in range(20):
fd_player = daily_dfs['dfsEntries'][0]['dfsRows'][i]['player']
salary_fd = daily_dfs['dfsEntries'][0]['dfsRows'][i]
fd_last_name = fd_player['LastName']
fd_first_name = fd_player['FirstName']
fd_player_salary = salary_fd['salary']
dk_player = daily_dfs['dfsEntries'][1]['dfsRows'][i]['player']
salary_dk = daily_dfs['dfsEntries'][1]['dfsRows'][i]
dk_last_name = dk_player['LastName']
dk_first_name = dk_player['FirstName']
dk_player_salary = salary_dk['salary']
writer.writerow([fanduel, fd_first_name, fd_last_name, fd_player_salary, draftkings, dk_first_name, dk_last_name, dk_player_salary])
print(fanduel, fd_first_name, fd_last_name, fd_player_salary, draftkings, dk_first_name, dk_last_name, dk_player_salary)
输出:
FanDuel Jonathan Quick 8800 DraftKings Martin Jones 8400
FanDuel Henrik Lundqvist 8600 DraftKings Aaron Dell 8300
FanDuel Frederik Andersen 8500 DraftKings Frederik Andersen 8200
FanDuel Martin Jones 8500 DraftKings Curtis McElhinney 8100
FanDuel Ben Bishop 8500 DraftKings Cory Schneider 7900
FanDuel Cory Schneider 8200 DraftKings Tyler Seguin 7800
答案 0 :(得分:2)
假设daily_dfs['dfsEntries'][0]['dfsRows']
和daily_dfs['dfsEntries'][1]['dfsRows']
具有相同数量的元素,您可以枚举第一个元素并在获取第二个元素时使用索引,即
for i, fd_info in enumerate(daily_dfs['dfsEntries'][0]['dfsRows']):
fd_player = fd_info['player']
fd_last_name = fd_player['LastName']
fd_first_name = fd_player['FirstName']
fd_player_salary = fd_info['salary']
dk_info = daily_dfs['dfsEntries'][1]['dfsRows'][i]
dk_player = dk_info['player']
dk_last_name = dk_player['LastName']
dk_first_name = dk_player['FirstName']
dk_player_salary = dk_info['salary']
# etc.
答案 1 :(得分:0)
library(neuralnet)
library(caret) #has the confusionmatrix function in it
#for some reason compute needs to be called like that, calling normally was producing some error
nnans <- neuralnet::compute(NN, test)
confusionMatrix(nnans, test_labels))