我有這段代碼可以迭代和決議來自 json API 提要的資料。我正在嘗試僅過濾掉英超聯賽,但有些比賽的名稱相似,例如“Premier League 2 (Division 2)”和“Premier League Cup”
for event_data in data['events']:
if event_data['path'][1]['name'] == 'England' and 'Premier League' in event_data['path'][2]['name'] and 'MATCH' in event_data['tags']:
competition = event_data['group']
competition_id = event_data['groupId']
event_id = event_data['id']
event_name = event_data['name']
home_team = event_data['homeName']
away_team = event_data['awayName']
event_start = event_data['start']
event_status = event_data['state']
print(f'{competition} {competition_id} {event_id} {event_name} {event_start} {event_status} {home_team} {away_team}')
此代碼的輸出如下。如您所見,前 2 行是與“英超聯賽”相似的比賽名稱。我嘗試按比賽 ID(第二列)進行過濾,但整數似乎會導致使用此方法出現問題。
Premier League 2 (Division 2) 2000096352 1019103797 Nottingham Forest U21 - Stoke City Reserves 2022-10-17T18:00:00Z NOT_STARTED Nottingham Forest U21 Stoke City Reserves
Premier League Cup 2000099592 1019095920 Middlesbrough U21 - West Bromwich Reserves 2022-10-17T17:00:00Z NOT_STARTED Middlesbrough U21 West Bromwich Reserves
Premier League 1000094985 1018752216 Brighton & Hove Albion - Nottingham Forest 2022-10-18T18:30:00Z NOT_STARTED Brighton & Hove Albion Nottingham Forest
Premier League 1000094985 1018752209 Crystal Palace - Wolves 2022-10-18T19:15:00Z NOT_STARTED Crystal Palace Wolves
Premier League 1000094985 1018752210 Bournemouth - Southampton 2022-10-19T18:30:00Z NOT_STARTED Bournemouth Southampton
Premier League 1000094985 1018752206 Brentford - Chelsea 2022-10-19T18:30:00Z NOT_STARTED Brentford Chelsea
Premier League 1000094985 1018752217 Liverpool - West Ham 2022-10-19T18:30:00Z NOT_STARTED Liverpool West Ham
Premier League 1000094985 1018752207 Newcastle United - Everton 2022-10-19T18:30:00Z NOT_STARTED Newcastle United Everton
Premier League 1000094985 1018752214 Manchester United - Tottenham 2022-10-19T19:15:00Z NOT_STARTED Manchester United Tottenham
Premier League 1000094985 1018752213 Fulham - Aston Villa 2022-10-20T18:30:00Z NOT_STARTED Fulham Aston Villa
Premier League 1000094985 1018752211 Leicester City - Leeds United 2022-10-20T19:15:00Z NOT_STARTED Leicester City Leeds United
Premier League 1000094985 1018752197 Nottingham Forest - Liverpool 2022-10-22T11:30:00Z NOT_STARTED Nottingham Forest Liverpool
Premier League 1000094985 1018752189 Everton - Crystal Palace 2022-10-22T14:00:00Z NOT_STARTED Everton Crystal Palace
Premier League 1000094985 1018752200 Manchester City - Brighton & Hove Albion 2022-10-22T14:00:00Z NOT_STARTED Manchester City Brighton & Hove Albion
Premier League 1000094985 1018752192 Chelsea - Manchester United 2022-10-22T16:30:00Z NOT_STARTED Chelsea Manchester United
Premier League 1000094985 1018752195 Aston Villa - Brentford 2022-10-23T13:00:00Z NOT_STARTED Aston Villa Brentford
Premier League 1000094985 1018752203 Leeds United - Fulham 2022-10-23T13:00:00Z NOT_STARTED Leeds United Fulham
Premier League 1000094985 1018752193 Southampton - Arsenal 2022-10-23T13:00:00Z NOT_STARTED Southampton Arsenal
Premier League 1000094985 1018752201 Wolves - Leicester City 2022-10-23T13:00:00Z NOT_STARTED Wolves Leicester City
Premier League 1000094985 1018752190 Tottenham - Newcastle United 2022-10-23T15:30:00Z NOT_STARTED Tottenham Newcastle United
Premier League 1000094985 1018752204 West Ham - Bournemouth 2022-10-24T19:00:00Z NOT_STARTED West Ham Bournemouth
從輸出中過濾這兩個比賽的最佳方法是什么?
Premier League 2 (Division 2) 2000096352 1019103797 Nottingham Forest U21 - Stoke City Reserves 2022-10-17T18:00:00Z NOT_STARTED Nottingham Forest U21 Stoke City Reserves
Premier League Cup 2000099592 1019095920 Middlesbrough U21 - West Bromwich Reserves 2022-10-17T17:00:00Z NOT_STARTED Middlesbrough U21 West Bromwich Reserves
這是 json 對于英超比賽的樣子:
{
"englishName": "Brighton & Hove Albion - Nottingham Forest",
"groupId": 1000094985,
"homeName": "Brighton & Hove Albion",
"groupSortOrder": 3001001000000000000,
"path":
[
{
"englishName": "Football",
"termKey": "football",
"name": "Soccer",
"id": 1000093190
},
{
"englishName": "England",
"termKey": "england",
"name": "England",
"id": 1000461733
},
{
"englishName": "Premier League",
"termKey": "premier_league",
"name": "Premier League",
"id": 1000094985
}
],
"id": 1018752216,
"state": "NOT_STARTED",
"group": "Premier League",
"start": "2022-10-18T18:30:00Z",
"nonLiveBoCount": 243,
"tags":
[
"OFFERED_LIVE",
"BET_BUILDER",
"MATCH"
],
"awayName": "Nottingham Forest",
"name": "Brighton & Hove Albion - Nottingham Forest",
"sport": "FOOTBALL"
}
這是'Premier League 2 (Division 2) 的 JSON
{
"englishName": "Nottingham Forest U21 - Stoke City Reserves",
"groupId": 2000096352,
"homeName": "Nottingham Forest U21",
"groupSortOrder": 3001999000000000000,
"path":
[
{
"englishName": "Football",
"termKey": "football",
"name": "Soccer",
"id": 1000093190
},
{
"englishName": "England",
"termKey": "england",
"name": "England",
"id": 1000461733
},
{
"englishName": "Premier League 2 (Division 2)",
"termKey": "premier_league_2__division_2_",
"name": "Premier League 2 (Division 2)",
"id": 2000096352
}
],
"id": 1019103797,
"state": "NOT_STARTED",
"group": "Premier League 2 (Division 2)",
"start": "2022-10-17T18:00:00Z",
"nonLiveBoCount": 14,
"tags":
[
"OFFERED_LIVE",
"BET_BUILDER",
"MATCH"
],
"awayName": "Stoke City Reserves",
"name": "Nottingham Forest U21 - Stoke City Reserves",
"sport": "FOOTBALL"
}
這是“英超聯賽杯”的 JSON
{
"englishName": "Middlesbrough U21 - West Bromwich Reserves",
"groupId": 2000099592,
"homeName": "Middlesbrough U21",
"groupSortOrder": 3001999000000000000,
"path":
[
{
"englishName": "Football",
"termKey": "football",
"name": "Soccer",
"id": 1000093190
},
{
"englishName": "England",
"termKey": "england",
"name": "England",
"id": 1000461733
},
{
"englishName": "Premier League Cup",
"termKey": "premier_league_cup",
"name": "Premier League Cup",
"id": 2000099592
}
],
"id": 1019095920,
"state": "NOT_STARTED",
"group": "Premier League Cup",
"start": "2022-10-17T17:00:00Z",
"nonLiveBoCount": 14,
"tags":
[
"OFFERED_LIVE",
"BET_BUILDER",
"MATCH"
],
"awayName": "West Bromwich Reserves",
"name": "Middlesbrough U21 - West Bromwich Reserves",
"sport": "FOOTBALL"
}
uj5u.com熱心網友回復:
看起來您可以更新條件以過濾 groupId 或組名
if event_data['path'][1]['name'] == 'England' and event_data['groupId'] == 1000094985 and 'MATCH' in event_data['tags']:
或者
if event_data['path'][1]['name'] == 'England' and event_data['group'] == 'Premier League' and 'MATCH' in event_data['tags']:
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/516865.html
標籤:Pythonjson
上一篇:在JSON物件中查找鍵并為其賦值。(Javascript)
下一篇:回圈和拆分物件
