Extracting Data from JSON In Python

Extracting Data from JSON

In this assignment you will write a Python program somewhat similar to http://www.py4e.com/code3/json2.py. The program will prompt for a URL, read the JSON data from that URL using urllib and 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.

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.

SOLUTION

import json
import urllib.request
count = 0
sum=0

url = " http://py4e-data.dr-chuck.net/comments_558264.json"
print ("retrieving URL. Stand by.")
uh = urllib.request.urlopen(url)
data= uh.read()

info = json.loads(str(data))

for i in info['comments']:
    count = count+1
    sum = sum + i['count']
print ("Sum : ",sum )  
print ("count : ",count)

Post a Comment

1 Comments

  1. I have been searching for a useful post like this on salesforce course details, it is highly helpful for me and I have a great experience with this Salesforce Training who are providing certification and job assistance.
    Salesforce training Hyderabad

    ReplyDelete