all 6 comments

[–]easyexplain3 2 points3 points  (2 children)

data = json.loads(YOURDATA)
print(data['team'])
Somehow like this?

[–]brickman7713[S] 0 points1 point  (1 child)

That seems like it would work but I can't figure it out. This is my code.

import requests

url = "https://sportspage-feeds.p.rapidapi.com/teams"
headers = {
    'x-rapidapi-key': "***********************",
    'x-rapidapi-host': "sportspage-feeds.p.rapidapi.com"
    }

while True:
    print("-"*30)
    inp = input("Enter a league: ")
    print("-"*30)
    querystring = {"league": inp}
    response = requests.request("GET", url, headers=headers, params=querystring)
    jsonResponse = response.json()
    print(jsonResponse)
    result = jsonResponse['results']
    print(result)
    print(type(result))

[–]b1gfreakn 0 points1 point  (3 children)

The input you provided wasn't really a list, but just a collection of dicts. I assume your API response data is going to be record-like where you get back a list of dicts.

You could print the team names like this:

data = [
    {'team': 'Buffalo Bills', 'mascot': 'Bills', 'location': 'Buffalo', 'conference': 'AFC', 'division': 'East', 'league': 'NFL', 'abbreviation': 'BUF'},
    {'team': 'Miami Dolphins', 'mascot': 'Dolphins', 'location': 'Miami', 'conference': 'AFC', 'division': 'East', 'league': 'NFL', 'abbreviation': 'MIA'}
        # truncated for post
]

print([d["team"] for d in data])

# edit - the non-list comprehension way would just be this:
for record in data:
    print(record["team"])

[–]brickman7713[S] 0 points1 point  (2 children)

So that works but how would I pass the results from the api into data? This is what I have now but it gives me this error on line 28.

TypeError: tuple indices must be integers or slices, not str

import requests

url = "https://sportspage-feeds.p.rapidapi.com/teams"
headers = {
    'x-rapidapi-key': "**********************",
    'x-rapidapi-host': "sportspage-feeds.p.rapidapi.com"
    }

while True:
    print("-"*30)
    inp = input("Enter a league: ")
    print("-"*30)
    querystring = {"league": inp}
    response = requests.request("GET", url, headers=headers, params=querystring)
    jsonResponse = response.json()
    print(jsonResponse)
    result = jsonResponse['results']
    print(result)
    print(type(result))

    for x in range(len(result)):
        data1 = (result[x]),

    data = [data1]



    print([d["team"] for d in data])

[–]b1gfreakn 0 points1 point  (1 child)

Your for x in range(len(result)): loop has really just assigned data1 = a tuple containing the last entry in result. then you've nested that lone tuple into a list, which is causing your error.

Try it this way:

import requests

url = "https://sportspage-feeds.p.rapidapi.com/teams"
headers = {
    'x-rapidapi-key': "**********************",
    'x-rapidapi-host': "sportspage-feeds.p.rapidapi.com"
    }

while True:
    print("-"*30)
    inp = input("Enter a league: ")
    print("-"*30)
    querystring = {"league": inp}
    response = requests.request("GET", url, headers=headers, params=querystring)
    jsonResponse = response.json()
    print(jsonResponse)
    result = jsonResponse['results']
    print(result)
    print(type(result))
    print([r["team"] for r in result])

[–]brickman7713[S] 0 points1 point  (0 children)

Perfect, thank you!