-2

I'm trying to use python to pull data from thebluealliance.com. This is my first attempt at doing this, and I was successful at getting some data from the site, however its not how I need it. Requests.json() is returning a list for me when I need a dictionary in order to parse based on keys. How would I go about converting it to a dictionary? I currently get an error saying that the indices need to be integers, not strings.

Heres the code im using to get the data.

response = requests.get(TBA_URL + '/event/2019pncmp/teams/simple?X-TBA-Auth-Key=' + TBA_AUTH_KEY)
    data = response.json()
    print(data['city']) //Error is here.

Example data:

[{'city': 'Issaquah', 'country': 'USA', 'key': 'frc1318', 
'name': 'The Boeing Company/State of Washington OSPI/Issaquah Schools Foundation&Issaquah High School', 
'nickname': 'Issaquah Robotics Society', 'state_prov': 'Washington', 
'team_number': 1318}, {'city': 'Wilsonville', 'country': 'USA', 
'key': 'frc1425', 'name': 'Lam Research/3D Systems/Xerox/DW Fritz/Rockwell Collins/TE Connectivity/Mentor Graphics/A-dec/City Of Wilsonville/Shields Manufacturing/Oregon Technology/Apex Plastics&Wilsonville High School', 
'nickname': 'Error Code Xero', 'state_prov': 'Oregon', 'team_number': 1425}, 
...]
David Zemens
  • 51,213
  • 11
  • 70
  • 118
  • can you show what your `data` looks like? – David Zemens May 10 '19 at 16:50
  • [{'city': 'Issaquah', 'country': 'USA', 'key': 'frc1318', 'name': 'The Boeing Company/State of Washington OSPI/Issaquah Schools Foundation&Issaquah High School', 'nickname': 'Issaquah Robotics Society', 'state_prov': 'Washington', 'team_number': 1318}, {'city': 'Wilsonville', 'country': 'USA', 'key': 'frc1425', 'name': 'Lam Research/3D Systems/Xerox/DW Fritz/Rockwell Collins/TE Connectivity/Mentor Graphics/A-dec/City Of Wilsonville/Shields Manufacturing/Oregon Technology/Apex Plastics&Wilsonville High School', 'nickname': 'Error Code Xero', 'state_prov': 'Oregon', 'team_number': 1425}, and on – Damon Larcom May 10 '19 at 16:54
  • `data` mirrors the structure of the JSON it's reading. For example, if the response is: `[{"a": {...}}]`, `data` is a list, with `list[0] == {'a': {...}}`. If the response is `{"a": "b", "c": "d"}`, `data` is a dict. – David Zemens May 10 '19 at 16:54
  • `for itm in data: print(itm['city'])` – David Zemens May 10 '19 at 16:56
  • Is there an easy way to convert from list to dict? I'd like to pull the data out using keys – Damon Larcom May 10 '19 at 16:56
  • The way is to iterate through the list and pull the value for key `city` for each dictionary @DamonLarcom – Devesh Kumar Singh May 10 '19 at 16:58
  • @DavidZemens thanks for the help, its working now! – Damon Larcom May 10 '19 at 17:03

1 Answers1

0

From the looks of it, data is a list of dictionaries, so you need to iterate through the list to pull the data in the dictionary

data=[{'city': 'Issaquah', 'country': 'USA', 'key': 'frc1318', 'name': 'The Boeing Company/State of Washington OSPI/Issaquah Schools Foundation&Issaquah High School', 'nickname': 'Issaquah Robotics Society', 'state_prov': 'Washington', 'team_number': 1318}, {'city': 'Wilsonville', 'country': 'USA', 'key': 'frc1425', 'name': 'Lam Research/3D Systems/Xerox/DW Fritz/Rockwell Collins/TE Connectivity/Mentor Graphics/A-dec/City Of Wilsonville/Shields Manufacturing/Oregon Technology/Apex Plastics&Wilsonville High School', 'nickname': 'Error Code Xero', 'state_prov': 'Oregon', 'team_number': 1425}]

#Iterate through the list and get value of city key from dictionary
for item in data:
    print(item['city'])

The output is

Issaquah
Wilsonville

Or using list-comprehension

res = [item['city'] for item in data]
print(res)

Output is ['Issaquah', 'Wilsonville']

Devesh Kumar Singh
  • 19,316
  • 5
  • 17
  • 37