Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #98563 > unrolled thread

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

Started byBernie Lazlo <bjlazlo@gmail.com>
First post2015-11-09 15:52 -0800
Last post2015-11-10 04:16 -0800
Articles 12 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  Extracting and summing student scores from a JSON file using Python 2.7.10 Bernie Lazlo <bjlazlo@gmail.com> - 2015-11-09 15:52 -0800
    Re: Extracting and summing student scores from a JSON file using Python 2.7.10 MRAB <python@mrabarnett.plus.com> - 2015-11-10 00:29 +0000
      Re: Extracting and summing student scores from a JSON file using Python 2.7.10 Bernie Lazlo <bjlazlo@gmail.com> - 2015-11-09 17:12 -0800
        Re: Extracting and summing student scores from a JSON file using Python 2.7.10 MRAB <python@mrabarnett.plus.com> - 2015-11-10 01:31 +0000
          Re: Extracting and summing student scores from a JSON file using Python 2.7.10 Bernie Lazlo <bjlazlo@gmail.com> - 2015-11-09 17:53 -0800
            Re: Extracting and summing student scores from a JSON file using Python 2.7.10 MRAB <python@mrabarnett.plus.com> - 2015-11-10 02:13 +0000
        Re: Extracting and summing student scores from a JSON file using Python 2.7.10 Pete Dowdell <contact@stridebird.com> - 2015-11-10 09:38 +0700
    Re: Extracting and summing student scores from a JSON file using Python 2.7.10 Bernie Lazlo <bjlazlo@gmail.com> - 2015-11-09 19:21 -0800
    Re: Extracting and summing student scores from a JSON file using Python 2.7.10 Denis McMahon <denismfmcmahon@gmail.com> - 2015-11-10 03:25 +0000
      Re: Extracting and summing student scores from a JSON file using Python 2.7.10 wayne.wickson@gmail.com - 2015-11-09 19:53 -0800
        Re: Extracting and summing student scores from a JSON file using Python 2.7.10 Bernie Lazlo <bjlazlo@gmail.com> - 2015-11-10 04:14 -0800
    Re: Extracting and summing student scores from a JSON file using Python 2.7.10 Bernie Lazlo <bjlazlo@gmail.com> - 2015-11-10 04:16 -0800

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

FromBernie Lazlo <bjlazlo@gmail.com>
Date2015-11-09 15:52 -0800
SubjectExtracting and summing student scores from a JSON file using Python 2.7.10
Message-ID<1d1610e5-d401-48f2-b5ee-3af91b9e485f@googlegroups.com>
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

[toc] | [next] | [standalone]


#98564

FromMRAB <python@mrabarnett.plus.com>
Date2015-11-10 00:29 +0000
Message-ID<mailman.194.1447115400.16136.python-list@python.org>
In reply to#98563
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.

[toc] | [prev] | [next] | [standalone]


#98565

FromBernie Lazlo <bjlazlo@gmail.com>
Date2015-11-09 17:12 -0800
Message-ID<a5ad1e84-cd46-44b7-a7d4-ed0f9b74ec92@googlegroups.com>
In reply to#98564
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.

[toc] | [prev] | [next] | [standalone]


#98566

FromMRAB <python@mrabarnett.plus.com>
Date2015-11-10 01:31 +0000
Message-ID<mailman.195.1447119088.16136.python-list@python.org>
In reply to#98565
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.

[toc] | [prev] | [next] | [standalone]


#98567

FromBernie Lazlo <bjlazlo@gmail.com>
Date2015-11-09 17:53 -0800
Message-ID<f8caed74-3069-4f49-b30c-4080a7199656@googlegroups.com>
In reply to#98566
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},

[toc] | [prev] | [next] | [standalone]


#98568

FromMRAB <python@mrabarnett.plus.com>
Date2015-11-10 02:13 +0000
Message-ID<mailman.196.1447121591.16136.python-list@python.org>
In reply to#98567
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.

[toc] | [prev] | [next] | [standalone]


#98581

FromPete Dowdell <contact@stridebird.com>
Date2015-11-10 09:38 +0700
Message-ID<mailman.201.1447145509.16136.python-list@python.org>
In reply to#98565
On 10/11/15 08:12, Bernie Lazlo wrote:
> > > import json
> > >import urllib
> > >url ="http://www.wickson.net/geography_assignment.json"
> > >response = urllib.urlopen(url)
> > >data = json.loads(response.read())

All good up to data. Now:
# make a list of scores
scores = [d['score'] for d in data['comments']  if 
isinstance(d['score'], int) ]
# analysis
total_scores = sum(scores)
average_score= total_scores/float(len(scores))
min_score, max_score = min(scores), max(scores)

pd

[toc] | [prev] | [next] | [standalone]


#98571

FromBernie Lazlo <bjlazlo@gmail.com>
Date2015-11-09 19:21 -0800
Message-ID<e34d03e9-7e80-4bbb-8910-d01ad4e9c950@googlegroups.com>
In reply to#98563
On Monday, 9 November 2015 18:53:06 UTC-5, 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
==========================

Many thanks, MRAB!  :-)

[toc] | [prev] | [next] | [standalone]


#98572

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2015-11-10 03:25 +0000
Message-ID<n1ro2l$n3t$1@dont-email.me>
In reply to#98563
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@gmail.com

[toc] | [prev] | [next] | [standalone]


#98573

Fromwayne.wickson@gmail.com
Date2015-11-09 19:53 -0800
Message-ID<9a9e1664-7b36-473d-bebf-3de7c7d13b94@googlegroups.com>
In reply to#98572
On Monday, 9 November 2015 22:27:40 UTC-5, Denis McMahon  wrote:
> 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
=========================================
Thanks for the reply, Denis. I hope this comes as easily to me some day. :-)

[toc] | [prev] | [next] | [standalone]


#98590

FromBernie Lazlo <bjlazlo@gmail.com>
Date2015-11-10 04:14 -0800
Message-ID<a3a0cf1d-1642-4dcd-93cd-760af13388ee@googlegroups.com>
In reply to#98573
On Monday, 9 November 2015 22:54:05 UTC-5, wayne....@gmail.com  wrote:
> On Monday, 9 November 2015 22:27:40 UTC-5, Denis McMahon  wrote:
> > 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
> =========================================
Thanks for the reply, Denis. I hope this comes as easily to me some day. :-) 

[toc] | [prev] | [next] | [standalone]


#98592

FromBernie Lazlo <bjlazlo@gmail.com>
Date2015-11-10 04:16 -0800
Message-ID<2c0e7cc9-c675-4f65-9aea-bf4c7ee13e64@googlegroups.com>
In reply to#98563
On Monday, 9 November 2015 18:53:06 UTC-5, 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

Pete, thanks for the input. Sometimes it just takes a suggestion to break past the "brick wall".

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web