【问题标题】:Extracting particular nested values within dictionary在字典中提取特定的嵌套值
【发布时间】:2021-02-12 01:46:19
【问题描述】:

对不起,如果我的措辞有误。我没有接受过 python 培训。

我请求了一个 JSON,并且可以手动提取我想要的数据。我知道有更好的方法来请求我想要的数据并且希望指向正确的方向。

这是我所拥有的(我只包含了 JSON 的一部分):

>   This is the example of the JSON that I am requesting.
  {"games": {
>       
>           "0":{
>     "id":"371749","gameUID":"1213028","sport":"Football","league":"NCAA","sportsbook":"BetOnline","startDate":"2020-10-29T23:30:01Z","awayRotationNumber":"103","homeRotationNumber":"104","awayTeam":"South
> Alabama","homeTeam":"Georgia Southern","description":"South Alabama vs
> Georgia
> Southern","isLive":"0","gameMoneylineAwayPrice":"+165","gameMoneylineHomePrice":"-190","gameSpreadAwayHandicap":"+4","gameSpreadHomeHandicap":"-4","gameSpreadAwayPrice":"-110","gameSpreadHomePrice":"-110","gameTotalPoints":"52","gameTotalOverPrice":"-110","gameTotalUnderPrice":"-110","gameTeamTotalAwayPoints":"24","gameTeamTotalAwayOverPrice":"-110","gameTeamTotalAwayUnderPrice":"-120","gameTeamTotalHomePoints":"28","gameTeamTotalHomeOverPrice":"-115","gameTeamTotalHomeUnderPrice":"-115","halfMoneylineAwayPrice":"+140","halfMoneylineHomePrice":"-160","halfSpreadAwayHandicap":"+3","halfSpreadHomeHandicap":"-3","halfSpreadAwayPrice":"-115","halfSpreadHomePrice":"-105","halfTotalPoints":"26","halfTotalOverPrice":"-115","halfTotalUnderPrice":"-105","periodMoneylineAwayPrice":"+135","periodMoneylineHomePrice":"-155","periodSpreadAwayHandicap":"+0.5","periodSpreadHomeHandicap":"-0.5","periodSpreadAwayPrice":"-105","periodSpreadHomePrice":"-115","periodTotalPoints":"10","periodTotalOverPrice":"-135","periodTotalUnderPrice":"+115","changedDate":"2020-10-29T14:31:16Z","checkedDate":"2020-10-29T15:13:33Z"}
>     ,"1":{(same as 0 etc etc...}

res = requests.get("myurl")

data = res.json()

game_one = data['games']['0']['awayTeam'] + " vs " + data['games']['0']['homeTeam'] + " " + data['games']['0']['gameSpreadHomeHandicap'] + "  "

game_two = data['games']['1']['awayTeam'] + " vs " + data['games']['1']['homeTeam'] + " " + data['games']['1']['gameSpreadHomeHandicap'] + "  "

game_three = data['games']['2']['awayTeam'] + " vs " + data['games']['2']['homeTeam'] + " " + 

ticker = game_one + game_two + game_three + game_four
output = open(r'E:\output.txt', 'w')
output.write(ticker)
output.close()

任何帮助将不胜感激。谢谢。

【问题讨论】:

    标签: python json distinct-values


    【解决方案1】:

    试试这个,它可能会起作用(如果您对代码有任何疑问 - 问他们):

    games = []
    for game in data['games']:
        for info in data['games'][game]:
            v1 = data['games'][game]['awayTeam'] + ' vs '
            v2 = data['games'][game]['homeTeam'] + ' '
            v3 = data['games'][game]['gameSpreadHomeHandicap']
        games.append(v1 + v2 + v3)
        
    with open('yourfile.txt', 'w') as file:
        file.write(' '.join(games))
    

    【讨论】:

    • 所以这会打印除垂直列表之外的所有格式。我将如何将这一切都集中在一条线上。我正在尝试将其输出到 LED 矩阵,以便它们像自动收报机一样滚动。
    • @cc2644 哇!我编辑了脚本,以便它这样做或至少应该这样做
    • @cc2644 实际上我更改了脚本,所以它更短(应该仍然有效)
    猜你喜欢
    • 2013-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-04
    • 2021-02-01
    • 2017-08-22
    • 2020-02-16
    相关资源
    最近更新 更多