[英]How to get all values for a key in the json, not just the last group?
The for loop only outputs the marketids for the last set of races of the json file. for 循环仅输出 json 文件的最后一组比赛的市场 ID。 I think this is because of the if statement preceding the for loop.
我认为这是因为 for 循环之前的 if 语句。 Anyone know how I can continue looping whilst keeping the if statement?
有人知道我如何在保持 if 语句的同时继续循环吗?
My code:我的代码:
request1 = requests.get('https://betfair-data-supplier-prod.herokuapp.com/api/daily_racing_results?date=2020-01-02')
json1 = request1.json()
marketids = []
for market in json1:
if market['raceType'] in ['R']:
marketids.append(market["markets"][0]["marketId"])
print(marketids)
Output of code: Output的代码:
['166897895', '166892549', '166896013', '166892651', '166892683', '166897912']
Example of json1: json1 示例:
[{'venueName': 'Geelong',
'raceDay': '2020-01-02 00:00:00',
'country': 'AUS',
'raceType': 'R',
'meetingId': '29637049',
'markets': [{'marketId': '166897895',
'raceNo': '1',
'startTime': '2020-01-02 02:00:00',
'marketStatus': 'CLOSED',
'eventName': 'Port Water Bouy Mdn Plate',
'nzToteEventId': '287012'},
{'marketId': '166897897',
'raceNo': '2',
'startTime': '2020-01-02 02:30:00',
'marketStatus': 'CLOSED',
'eventName': 'Portarlington Beach Motel Mdn',
'nzToteEventId': '287013'},
{'marketId': '166897899',
'raceNo': '3',
'startTime': '2020-01-02 03:00:00',
'marketStatus': 'CLOSED',
'eventName': 'Flash Fabrications Mdn Plat3',
'nzToteEventId': '287014'},
{'marketId': '166897901',
'raceNo': '4',
'startTime': '2020-01-02 03:30:00',
'marketStatus': 'CLOSED',
'eventName': 'Race With Empower Racing Mdn',
'nzToteEventId': '287015'},
{'marketId': '166897903',
'raceNo': '5',
'startTime': '2020-01-02 04:00:00',
'marketStatus': 'CLOSED',
'eventName': 'Absolute Landscapes Mdn Plate',
'nzToteEventId': '287016'},
{'marketId': '166897905',
'raceNo': '6',
'startTime': '2020-01-02 04:30:00',
'marketStatus': 'CLOSED',
'eventName': 'Ajk Builders (Bm64)',
'nzToteEventId': '287017'},
{'marketId': '166933411',
'raceNo': '7',
'startTime': '2020-01-02 05:00:00',
'marketStatus': 'CLOSED',
'eventName': 'Batman Mgmt Bellarine Cup-Bm64',
'nzToteEventId': '287018'},
{'marketId': '166897909',
'raceNo': '8',
'startTime': '2020-01-02 05:30:00',
'marketStatus': 'CLOSED',
'eventName': 'Gj Bradding Heating&Cool.-Bm58',
'nzToteEventId': '287019'},
{'marketId': '166897911',
'raceNo': '9',
'startTime': '2020-01-02 06:00:00',
'marketStatus': 'CLOSED',
'eventName': 'Bayshore Electrical (Bm58)',
'nzToteEventId': '287020'}]},
{'venueName': 'Mount Barker',
'raceDay': '2020-01-02 00:00:00',
'country': 'AUS',
'raceType': 'R',
'meetingId': '29636945',
'markets': [{'marketId': '166892549',
'raceNo': '1',
'startTime': '2020-01-02 05:17:00',
'marketStatus': 'CLOSED',
'eventName': 'Happy New Year Mdn',
'nzToteEventId': ''},
{'marketId': '166892551',
'raceNo': '2',
'startTime': '2020-01-02 05:52:00',
'marketStatus': 'CLOSED',
'eventName': 'Langton Mdn',
'nzToteEventId': ''},
{'marketId': '166892553',
'raceNo': '3',
'startTime': '2020-01-02 06:32:00',
'marketStatus': 'CLOSED',
'eventName': 'McDonald Hcp (C2)',
'nzToteEventId': ''},
{'marketId': '166892555',
'raceNo': '4',
'startTime': '2020-01-02 07:10:00',
'marketStatus': 'CLOSED',
'eventName': 'Hicks Hcp (C2)',
'nzToteEventId': ''},
{'marketId': '166892557',
'raceNo': '5',
'startTime': '2020-01-02 07:45:00',
'marketStatus': 'CLOSED',
'eventName': 'Lowood (Bm58+)',
'nzToteEventId': ''}]}]
request1 = requests.get('https://betfair-data-supplier-prod.herokuapp.com/api/daily_racing_results?date=2020-01-02')
json1 = request1.json()
marketids = []
for market in json1:
if market['raceType'] in ['R']:
for id in market['markets']:
marketids.append(market["markets"][id]["marketId"])
print(marketids)
edit solution by @Sushanth @Sushanth 编辑解决方案
[i['marketId'] for v in request1.json() if v['raceType'] == 'R' for i in v['markets']]
pandas.json_normalize
pandas.json_normalize
marketId
in a list:marketId
在列表中:
df.marketId.to_list()
markets
markets
import pandas as pd
import requests
request1 = requests.get('https://betfair-data-supplier-prod.herokuapp.com/api/daily_racing_results?date=2020-01-02')
json1 = request1.json()
df = pd.json_normalize(json1, 'markets')
# display(df.head()
marketId raceNo startTime marketStatus eventName nzToteEventId
0 166897895 1 2020-01-02 02:00:00 CLOSED Port Water Bouy Mdn Plate 287012
1 166897897 2 2020-01-02 02:30:00 CLOSED Portarlington Beach Motel Mdn 287013
2 166897899 3 2020-01-02 03:00:00 CLOSED Flash Fabrications Mdn Plat3 287014
3 166897901 4 2020-01-02 03:30:00 CLOSED Race With Empower Racing Mdn 287015
4 166897903 5 2020-01-02 04:00:00 CLOSED Absolute Landscapes Mdn Plate 287016
keys
keys
df = pd.json_normalize(json1, 'markets', ['venueName', 'raceDay', 'raceType', 'meetingId'])
# display(df.head())
marketId raceNo startTime marketStatus eventName nzToteEventId venueName raceDay raceType meetingId
0 166897895 1 2020-01-02 02:00:00 CLOSED Port Water Bouy Mdn Plate 287012 Geelong 2020-01-02 00:00:00 R 29637049
1 166897897 2 2020-01-02 02:30:00 CLOSED Portarlington Beach Motel Mdn 287013 Geelong 2020-01-02 00:00:00 R 29637049
2 166897899 3 2020-01-02 03:00:00 CLOSED Flash Fabrications Mdn Plat3 287014 Geelong 2020-01-02 00:00:00 R 29637049
3 166897901 4 2020-01-02 03:30:00 CLOSED Race With Empower Racing Mdn 287015 Geelong 2020-01-02 00:00:00 R 29637049
4 166897903 5 2020-01-02 04:00:00 CLOSED Absolute Landscapes Mdn Plate 287016 Geelong 2020-01-02 00:00:00 R 29637049
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.