Skip to content

Instantly share code, notes, and snippets.

@AreebaYousuf
Created May 25, 2023 07:57
Show Gist options
  • Save AreebaYousuf/637d84bbbaadb76b5328b46f10ea27c3 to your computer and use it in GitHub Desktop.
Save AreebaYousuf/637d84bbbaadb76b5328b46f10ea27c3 to your computer and use it in GitHub Desktop.
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:…
import urllib.request, urllib.parse, urllib.error
import json
Sum=0
count=0
inp=input('Enter location:')
print('Retrieving ',inp)
uh = urllib.request.urlopen(inp)
data = uh.read()
info = json.loads(data)
print('Retrieved ', len(data),'characters')
for item in info['comments']:
count=1+count
t=int(item['count'])
Sum=Sum+t
print('Count:',count)
print('Sum:',Sum)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment