使用Python访问网络数据 python network-data 第六章

question:

Extracting Data from JSON

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.

 

Extracting Data from JSON

In this assignment you will write a Python program somewhat similar to http://www.pythonlearn.com/code/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.

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. Data Format

The data consists of a number of names and comment counts in JSON as follows:

{
  comments: [
    {
      name: "Matthias"
      count: 97
    },
    {
      name: "Geomer"
      count: 97
    }
    ...
  ]
}

The closest sample code that shows how to parse JSON and extract a list is json2.py. You might also want to look at geoxml.py to see how to prompt for a URL and retrieve data from a URL.

Sample Execution

$ python solution.py 
Enter location: http://python-data.dr-chuck.net/comments_42.json
Retrieving http://python-data.dr-chuck.net/comments_42.json
Retrieved 2733 characters
Count: 50
Sum: 2...

Turning in the Assignment

Enter the sum from the actual data and your Python code below:
Sum: (ends with 69)


Python code:

 

My Code

 1 import urllib
 2 import json
 3 
 4 # url = 'http://python-data.dr-chuck.net/comments_265446.json'
 5 while True:
 6     address = raw_input('Enter location: ')
 7     if len(address) < 1 : break
 8     print 'Retrieving', address
 9     uh = urllib.urlopen(address)
10     data = uh.read()
11     print 'Retrieved',len(data),'characters'
12     input = urllib.urlopen(address).read()
13     js = json.loads(input)
14     sum = 0
15     for dict in js['comments']:
16         sum += int(dict['count'])
17     print 'Total count:', len(js.get('comments',[]))    
18     print 'sum: ', sum
19     break
View Code

运行结果

 

学习心得:

1.count的长度不会使用,在stackoverflow上借鉴了len(js.get('comments',[]))  这个语句。

http://stackoverflow.com/questions/34807054/extracting-data-from-dictionary-in-python

2.整体代码学习源

 1 import urllib
 2 import json
 3 
 4 url = 'http://python-data.dr-chuck.net/comments_194528.json'
 5 
 6 input = urllib.urlopen(url).read()
 7 js = json.loads(input)
 8 sum = 0
 9 for dict in js['comments']:
10     sum += int(dict['count'])
11 print sum

 

http://www.cnblogs.com/wanderingzj/p/5010460.html

 

转载于:https://www.cnblogs.com/Gailsunset/p/5596237.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值