Geeks of Coding

Join us on Telegram

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

      Extracting Data from JSON

      In this assignment you will write a Python program somewhat similar to 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: (Sum=2553)
      Actual data: (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:
      #Retrieved 2711 characters
      #Count: 50
      #Sum: 2365
      import urllib.request as ur
      import json
      # json_url = ''
      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.