4

I am new to python and facing this error: string indices must be integers while parsing a JSON file.

JSON File:

{"NFLTeams": [
  {"code":"ARI","fullName":"Arizona Cardinals","shortName":"Arizona"},
  {"code":"ATL","fullName":"Atlanta Falcons","shortName":"Atlanta"},
  {"code":"WAS","fullName":"Washington Redskins","shortName":"Washington"}
]}

My code:

import urllib.request as ur
import urllib.parse
import json

url = 'http://www.fantasyfootballnerd.com/service/nfl-teams/json/test/'
user_agent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)'
values = {'name' : 'Michael Foord',
          'location' : 'Northampton',
          'language' : 'Python' }
headers = { 'User-Agent' : user_agent }

data = urllib.parse.urlencode(values)
data = data.encode('ascii')
req = urllib.request.Request(url, data, headers)
response = urllib.request.urlopen(req)
the_page = response.read().decode('utf-8')

print (the_page)
team_data = json.loads(the_page)

print (type(team_data))  // team_data is type dict

for item in team_data:
    print (item['NFLTeams'][0]["code"])
    print (item['NFLTeams'][0]['fullName'])
    print (item['NFLTeams'][0]['shortName'])

I have tried to print the following:

print (item['NFLTeams']["code"]) 

And this:

for item in team_data['NFLTeams'].values():
    print (item["code"])
    print (item['fullName'])
    print (item['shortName'])

Which gives me this error:

for item in team_data['NFLTeams'].values():
AttributeError: 'list' object has no attribute 'values'

Can anyone please help me figure out what's going on? Thank you.

3
  • team_data['NFLTeams'] is a list, not a dict, and therefore does not have a values method. You should print the entire team_data object to see what it actually is, and then you can work out how to access it. Commented Jan 17, 2016 at 0:56
  • When asking python question on Stack Overflow, please specify which version of python you are using, and tag your question either python-2 or python-3. It may not be relevant to this question, but it generally helps. Commented Jan 17, 2016 at 1:19
  • thank you for bringing that up. I will keep it in mind while posting questions next time Commented Jan 17, 2016 at 2:36

1 Answer 1

3

.values() is used to loop through the values of a dictionary however, team_data['NFLTeams'] is a list containing dictionaries. Thus, you need to remove .values() to access each dictionary whilst iterating:

for item in team_data['NFLTeams']:
    print (item["code"])
    print (item['fullName'])
    print (item['shortName'])

If you really want to use .values():

for item in team_data.values()[0]:
    print (item["code"])
    print (item['fullName'])
    print (item['shortName'])

Please keep in mind that .values() returns a view object in Python 3.x, so you need to force evaluating it using list() in order to access the elements by index:

for item in list(team_data.values())[0]:
Sign up to request clarification or add additional context in comments.

3 Comments

You can't do [0] with a values view.
@TigerhawkT3 I assumed he is using Python 2.x despite the usage of print
Oh, one of the old eager functions, got it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.