Created
May 25, 2023 07:57
-
-
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:…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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