读取data
将里面count的数字都抓出来,并加和。
data={
"note":"This file contains the actual data for your assignment",
"comments":[
{
"name":"Lilygrace",
"count":99
},
{
"name":"Fraser",
"count":1
}
]
}
思考过程及代码
用json把数字抓出来
import urllib.request, urllib.parse, urllib.error
import ssl
import json
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
url= input('Enter: ')
uh = urllib.request.urlopen(url, context=ctx)
data = uh.read()
info = json.loads(data)
x3 = []
for item in info:
x1 = item['count']
x3.append(int(x1))
print(x3)
print(sum(x3))
执行情况
报错 x1 = item['count']
TypeError: string indices must be integers