Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: MRAB Newsgroups: comp.lang.python Subject: Re: Extracting and summing student scores from a JSON file using Python 2.7.10 Date: Tue, 10 Nov 2015 02:13:00 +0000 Lines: 43 Message-ID: References: <1d1610e5-d401-48f2-b5ee-3af91b9e485f@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de IofS07CLXY+eBNK39rPjMASIbE194lTZbXf6MCNCK78A== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'essentially': 0.04; 'subject:Python': 0.05; 'json': 0.05; 'mrab': 0.05; 'removes': 0.05; 'indexing': 0.07; 'subject:file': 0.07; '===========': 0.09; 'item,': 0.09; 'subject:using': 0.09; 'tuple': 0.09; 'tuple.': 0.09; 'index': 0.13; 'subject: \n ': 0.15; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'received:192.168.1.4': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:JSON': 0.16; 'subject:student': 0.16; 'url:json': 0.16; 'urllib': 0.16; 'wrote:': 0.16; 'string': 0.17; 'student': 0.20; '2015': 0.20; 'produces': 0.22; 'skip:= 20': 0.22; 'tuples': 0.22; 'appears': 0.23; 'second': 0.24; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User- Agent:1': 0.26; 'appreciated.': 0.27; 'closer': 0.29; 'print': 0.30; 'code': 0.30; 'received:84': 0.32; 'problem': 0.33; 'extract': 0.33; 'skip:j 20': 0.33; 'file': 0.34; 'list': 0.34; 'gets': 0.35; 'gives': 0.35; 'lists.': 0.35; 'step': 0.36; 'but': 0.36; 'list,': 0.36; 'should': 0.36; 'monday,': 0.36; 'to:addr :python-list': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'list.': 0.37; 'data': 0.39; 'subject:from': 0.39; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'skip:u 10': 0.61; 'further': 0.62; 'back': 0.62; 'hours': 0.65; 'dict.': 0.84; 'closer.': 0.91; 'items,': 0.91; 'scores': 0.91 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=MbeRwMLf c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=EBOSESyhAAAA:8 a=IkcTkHD0fZMA:10 a=vFvQ7Ah-AAAA:8 a=g_EHh6WjY80q_JxUMq0A:9 a=QEXdDO2ut3YA:10 X-AUTH: mrabarnett@:2500 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:98568 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.