Peter Hartnett Peter Hartnett - 2 months ago 8
Python Question

Searching Dictionary in list

I am working on my first project with API's and I am having trouble accessing the data. I am working off an example that calls its data with this loop:

for item in data['objects']:
print item['name'], item['phone']


This works great for data stored as nested dictionaries (the outside being called objects, and the inside containing the data)

The issue I am having is my data is formatted with dictonaries inside of lists

[
{
"key":"2014cama",
"website":"http://www.cvrobotics.org/frc/regional.html",
"official":true,
"end_date":"2014-03-09",
"name":"Central Valley Regional",
"short_name":"Central Valley",
"facebook_eid":null,
"event_district_string":null,
"venue_address":"Madera South High School\n705 W. Pecan Avenue\nMadera, CA 93637\nUSA",
"event_district":0,
"location":"Madera, CA, USA",
"event_code":"cama",
"year":2014,
"webcast":[],
"timezone":"America/Los_Angeles",
"alliances":[],
"event_type_string":"Regional",
"start_date":"2014-03-07",
"event_type":0
},'more data...']


so calling,

for item in data['objects']:
print item['name']


Won't work to pull the value stored in
name
.

Any help would be much appreciated.

Edit: The full Dataset I'm pulling (http://www.thebluealliance.com/api/v2/team/frc254/2014/events?X-TBA-App-Id=Peter_Hartnett:Scouting:v1)

And the code I am running:

import json,urllib2, TBA

team ='frc254'
year = '2014'



Url = 'http://www.thebluealliance.com/api/v2/team/'+team+'/'+year+'/events?X- TBA-App-Id=Peter_Hartnett:Scouting:v1'
data = TBA.GetData(Url)

for item in data:
print item['name']


The TBA Class just imports the data and returns it.

Edit2:
Here is the TBA class that pulls the data, I can assure you it is identical to that found at the link above

import urllib2,cookielib
content='none'


def GetData(Url):
site= Url
hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
'Accept': ' 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
'Accept-Encoding': 'none',
'Accept-Language': 'en-US,en;q=0.8',
'Connection': 'keep-alive'}

req = urllib2.Request(site, headers=hdr)

try:
page = urllib2.urlopen(req)
except urllib2.HTTPError, e:
print e.fp.read()

content = page.read()
return content

Answer

If I understood it correctly, your data['objects'] is now an entry in a list, right?

So just iterate the list and your logic will remain the same

for item in objects:
     print item['name'], item['phone']

being

objects = [
{
"key":"2014cama",
"website":"http://www.cvrobotics.org/frc/regional.html",
"official":true,
"end_date":"2014-03-09",
"name":"Central Valley Regional",
"short_name":"Central Valley",
"facebook_eid":null,
"event_district_string":null,
"venue_address":"Madera South High School\n705 W. Pecan Avenue\nMadera, CA 93637\nUSA",      
 "event_district":0,
"location":"Madera, CA, USA",
"event_code":"cama",
"year":2014,
"webcast":[],
"timezone":"America/Los_Angeles",
"alliances":[],
"event_type_string":"Regional",
"start_date":"2014-03-07",
"event_type":0
},'more data...']

Edit

I get your problem now. Your object data is a string that represents a JSONArray . You should load that before iterating, in order to be able to work with that as a real list, like so:

data = GetData(Url)

loaded_array = json.loads(data)

for item in loaded_array:
     print item['name']
Comments