Phone

+919997782184

Email

support@roboticswithpython.com

Geeks of Coding

Join us on Telegram

Viewing 0 reply threads
  • Author
    Posts
    • #894
      Abhishek TyagiAbhishek Tyagi
      Keymaster

      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.

      Sample data: http://py4e-data.dr-chuck.net/comments_42.json (Sum=2553)
      Actual data: http://py4e-data.dr-chuck.net/comments_97411.json (Sum ends with 65)
      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.

      #Enter location: http://py4e-data.dr-chuck.net/comments_97411.json
      #Retrieving  http://py4e-data.dr-chuck.net/comments_97411.json
      #Retrieved 2711 characters
      #Count: 50
      #Sum: 2365
      
      import urllib.request as ur
      import json
      
      # json_url = 'http://python-data.dr-chuck.net/comments_42.json'
      
      json_url = input("Enter location: ")
      print("Retrieving ", json_url)
      data = ur.urlopen(json_url).read().decode('utf-8')
      print('Retrieved', len(data), 'characters')
      json_obj = json.loads(data)
      
      sum = 0
      total_number = 0
      
      for comment in json_obj["comments"]:
          sum += int(comment["count"])
          total_number += 1
      
      print('Count:', total_number)
      print('Sum:', sum)
Viewing 0 reply threads
  • You must be logged in to reply to this topic.