您的位置:首页 > 编程语言 > Python开发

Python.Extracting Data from JSON

2016-07-24 20:05 489 查看
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

JSON地址:http://python-data.dr-chuck.net/comments_290549.json (Sum
ends with 90)

Python源码:

import urllib
import json

inp = raw_input('Enter location:')
url = urllib.urlopen(inp).read()
data = json.loads(url)

info = data['comments']
leng = len(info)
sum = 0
for i in range(leng):
num = info[i-1]['count']
sum = sum + num
print sum

运行结果:
Enter location:http://python-data.dr-chuck.net/comments_290549.json
2790
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: