Extracting and summing student scores from a JSON file using Python 2.7.10

MRAB python at mrabarnett.plus.com
Mon Nov 9 21:13:00 EST 2015


On 2015-11-10 01:53, Bernie Lazlo wrote:
> On Monday, 9 November 2015 20:31:52 UTC-5, MRAB  wrote:
>> On 2015-11-10 01:12, Bernie Lazlo wrote:
>> > On Monday, 9 November 2015 19:30:23 UTC-5, MRAB  wrote:
>> >> On 2015-11-09 23:52, Bernie Lazlo wrote:
>> >> > This should be a simple problem but I have wasted hours on it. Any help would be appreciated. [I have taken my code back to almost the very beginning.]
>> >> > ========================
>> >> > The student scores need to be summed.
>> >> > ========================
>> >> > import json
>> >> > import urllib
>> >> > url = "http://www.wickson.net/geography_assignment.json"
>> >> > response = urllib.urlopen(url)
>> >> > data = json.loads(response.read())
>> >> > lst1 = list(data.items())
>> >> > print lst1
>> >> >
>> >> Do it a step at a time.
>> >>
>> >> It's a list, so start with indexing.
>> >
>> > MRAB:
>> >
>> > I think of the file as two lists. The second list appears to be a list of tuples containing "names" and "scores". How would you index or extract those.
>> >
>> Right, so lst1[1] gets you closer to what you want.
>>
>> Further indexing will get you even closer.
> ===========
> lst2 = lst1[1] removes first line of  instructions
>
> printing lst2[1:2] produces essentially the list of students and scores ??
>
> ([{u'student ': u'Hannah', u'score': 77}, {u'student ': u'Emily', u'score': 57}, {u'student ': u'Olivia', u'score': 80}, {u'student ': u'Nora', u'score': 70},
>
lst1 is 1 list of 2 items, both of which are tuples. lst1[1] gives you
the second tuple.

That tuple contains 2 items, the first a string and the second a list.
You want the second item, so that's lst1[1][1].

Each of the items in that list is a dict.




More information about the Python-list mailing list