如何获取 json 中某个键的所有值,而不仅仅是最后一组?
How to get all values for a key in the json, not just the last group?
for 循环只输出 json 文件最后一组比赛的 marketid。我认为这是因为 for 循环之前的 if 语句。任何人都知道如何在保持 if 语句的同时继续循环?
我的代码:
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)
代码输出:
['166897895', '166892549', '166896013', '166892651', '166892683', '166897912']
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)
@Sushant 编辑解决方案
[i['marketId'] for v in request1.json() if v['raceType'] == 'R' for i in v['markets']]
使用pandas.json_normalize
- 将半结构化 JSON 数据标准化为平面 table。
- 更少的代码行
- 所有数据都采用易于使用的格式。
- 如果您仍希望列表中有
marketId
:
df.marketId.to_list()
解压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
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
for 循环只输出 json 文件最后一组比赛的 marketid。我认为这是因为 for 循环之前的 if 语句。任何人都知道如何在保持 if 语句的同时继续循环?
我的代码:
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)
代码输出:
['166897895', '166892549', '166896013', '166892651', '166892683', '166897912']
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)
@Sushant 编辑解决方案
[i['marketId'] for v in request1.json() if v['raceType'] == 'R' for i in v['markets']]
使用pandas.json_normalize
- 将半结构化 JSON 数据标准化为平面 table。
- 更少的代码行
- 所有数据都采用易于使用的格式。
- 如果您仍希望列表中有
marketId
:df.marketId.to_list()
解压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
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