Question: Extracting Data from JSON A Python program somewhat similar tohttp://www.py4e.com/code3/json2.py. The program will prompt for a URL, read the JSON data from that URL using
Extracting Data from JSON
A Python program somewhat similar tohttp://www.py4e.com/code3/json2.py. The program will prompt for a URL, read the JSON data from that URL usingurlliband then parse and extract the comment counts from the JSON data, compute the sum of the numbers in the file and enter the sum below:
We provide two files for this assignment. One is a sample file where we give you the sum for your testing and the other is the actual data you need to process for the assignment.
- Sample data:http://py4e-data.dr-chuck.net/comments_42.json(Sum=2553)
- Actual data:http://py4e-data.dr-chuck.net/comments_525430.json(Sum ends with 28)
You do not need to save these files to your folder since your program will read the data directly from the URL.Note:Each student will have a distinct data url for the assignment - so only use your own data url for analysis.Data FormatThe data consists of a number of names and comment counts in JSON as follows:
{ comments: [ { name: "Matthias" count: 97 }, { name: "Geomer" count: 97 } ... ]}The closest sample code that shows how to parse JSON and extract a list isjson2.py. You might also want to look atgeoxml.pyto see how to prompt for a URL and retrieve data from a URL.
Sample Execution
$ python3 solution.pyEnter location: http://py4e-data.dr-chuck.net/comments_42.jsonRetrieving http://py4e-data.dr-chuck.net/comments_42.jsonRetrieved 2733 charactersCount: 50Sum: 2...
ALL THE ABOVE TEXT IS QUESTION
NOW ITS MY SOLUTION:
import json
import urllib.request
url=input('Enter Location:')
print ('Retrieving',url)
input = urllib.request.urlopen(url).read().decode('utf-8')
print ('Retrieved', len(input), 'characters')
info=json.loads(input)
cnt=0
sum=0
for i in range(0,len(info['comments'])):
cnt=cnt+1
sum=sum+int(info['comments'][i]['count'])
print('Count=', cnt)
print('Sum=',sum)
THERE IS A ATTRIBUTE ERROR . HOW TO OVERCOME IT

\f
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
