Extracting and summing student scores from a JSON file using Python 2.7.10
Denis McMahon
denismfmcmahon at gmail.com
Mon Nov 9 22:25:09 EST 2015
On Mon, 09 Nov 2015 15:52:45 -0800, 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
I find that pprint.pprint is useful for looking at data structures.
Having looked at the data, and then using appropriate substitutions for
<something> and <keyval> in the following:
sumscore = 0
students = 0
for dic in <something>:
sumscore = sumscore + dic[<keyval>]
students += 1
print 'Sum of', students, 'scores is', sumscore
print 'Average of', students, 'scores is', sumscore / students
It was trivial to generate:
Sum of 50 scores is 3028
Average of 50 scores is 60
--
Denis McMahon, denismfmcmahon at gmail.com
More information about the Python-list
mailing list