在Python的大数组中寻找“打印”的位置?

我正在学习如何从数组中获取数据,并且我有点陷入一种简单的方法来定位数据从数组中提取数据的位置。感觉应该有比在屏幕上数更简单的方法。


这是我所拥有的:


r2 = requests.get(

    f'https://www.thesportsdb.com/api/v1/json/{apiKey}/lookupevent.php?id={id}')


arr_events = np.array([r2.json()])


#print(arr_events)


event_id = arr_events[0]['events'][0]['idEvent']

locate = arr_events.index('strHomeTeam')

print(locate)

问题是,在控制台上打印出一个巨大的数组,看起来像(我将给出一行,你可能明白了):


[{'events': [{'idEvent': '1032723', 'idSoccerXML': None, 'idAPIfootball': '592172', 'strEvent': 'Aston Villa vs Liverpool', 'strEventAlternate': 'Liverpool @ Aston Villa', 'strFilename': 'English Premier League 2020-10-04 Aston Villa vs Liverpool'...}]}]

这是一个相当大的数组,如果我需要提取一些信息,足以导致轻微的速度减慢。


因此,使用上面的方法很容易拉取 idEvent。如果我想要其中一些位于顶行,可能不难数到 5 或 6。但我知道 Python 必须有一种更简单的方法来定位我想要的那些。例如,我想要主队和客队:


'strHomeTeam': 'Aston Villa', 'strAwayTeam': 'Liverpool',

那么有没有一种更简单的方法来拉动“strHomeTeam”而不是一直计数到数组中的点呢?


我意识到这是一个基本问题 - 我已经搜索了又搜索,但所有内容似乎都在一个非常小的数组中,并且它们似乎没有解释如何轻松地从大数组中获取数据。


JSON 文件位于:https://www.thesportsdb.com/api/v1/json/1/lookupevent.php ?id=1032723

感谢您对此的帮助 - 我很感激。


天涯尽头无女友
浏览 147回答 3
3回答

慕斯709654

那么有没有一种更简单的方法来拉动“strHomeTeam”而不是一直计数到数组中的点呢?尝试下面的方法data = {"events": [    {"idEvent": "1032723", "idSoccerXML": "", "idAPIfootball": "592172", "strEvent": "Aston Villa vs Liverpool",     "strEventAlternate": "Liverpool @ Aston Villa",     "strFilename": "English Premier League 2020-10-04 Aston Villa vs Liverpool", "strSport": "Soccer",     "idLeague": "4328", "strLeague": "English Premier League", "strSeason": "2020-2021",     "strDescriptionEN": "Aston Villa and Liverpool square off at Villa Park, where last season, these teams produced one of the most exciting finishes of the campaign, as Liverpool scored twice late on to overturn an early Trezeguet goal.",     "strHomeTeam": "Aston Villa", "strAwayTeam": "Liverpool", "intHomeScore": "7", "intRound": "4",     "intAwayScore": "2", "intSpectators": "", "strOfficial": "", "strHomeGoalDetails": "", "strHomeRedCards": "",     "strHomeYellowCards": "", "strHomeLineupGoalkeeper": "", "strHomeLineupDefense": "",     "strHomeLineupMidfield": "", "strHomeLineupForward": "", "strHomeLineupSubstitutes": "",     "strHomeFormation": "", "strAwayRedCards": "", "strAwayYellowCards": "", "strAwayGoalDetails": "",     "strAwayLineupGoalkeeper": "", "strAwayLineupDefense": "", "strAwayLineupMidfield": "",     "strAwayLineupForward": "", "strAwayLineupSubstitutes": "", "strAwayFormation": "", "intHomeShots": "",     "intAwayShots": "", "strTimestamp": "2020-10-04T18:15:00+00:00", "dateEvent": "2020-10-04",     "dateEventLocal": "2020-10-04", "strDate": "", "strTime": "18:15:00", "strTimeLocal": "19:15:00",     "strTVStation": "", "idHomeTeam": "133601", "idAwayTeam": "133602", "strResult": "", "strVenue": "Villa Park",     "strCountry": "England", "strCity": "", "strPoster": "", "strFanart": "",     "strThumb": "https:\/\/www.thesportsdb.com\/images\/media\/event\/thumb\/r00vzl1601721606.jpg", "strBanner": "",     "strMap": "", "strTweet1": "https:\/\/twitter.com\/brfootball\/status\/1312843172385521665",     "strTweet2": "https:\/\/twitter.com\/TomJordan21\/status\/1312854281444306946",     "strTweet3": "https:\/\/twitter.com\/FutbolBible\/status\/1312847622592442370",     "strVideo": "https:\/\/www.youtube.com\/watch?v=0Nbw3jSafGM", "strStatus": "Match Finished", "strPostponed": "no",     "strLocked": "unlocked"}]}filtered_data = [{'home':entry['strHomeTeam'],'away':entry['strAwayTeam']}for entry in data['events']]print(filtered_data)输出[{'home': 'Aston Villa', 'away': 'Liverpool'}]

慕无忌1623718

呃......我尝试了一些不同的东西,它起作用了 - 叹息......我很抱歉。event_id = arr_events[0]['events'][0]['idEvent']home_team = arr_events[0]['events'][0]['strHomeTeam']away_team = arr_events[0]['events'][0]['strAwayTeam']home_score = arr_events[0]['events'][0]['intHomeScore']away_score = arr_events[0]['events'][0]['intAwayScore']我认为这是正确的方法。

白猪掌柜的

您应该查看 https://python-json-pointer.readthedocs.io/en/latest/tutorial.html检查 json,获取要访问值的路径 -> 使用https://github.com/stefankoegl/python-json-pointer
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python