python字符串索引必须是整数_TypeError:字符串索引必须是整数Python

我发现了一个“打字错误”,我不知道该怎么解决了。拜托,帮帮忙。如果可能的话,我将非常感谢你的解释。在

我的代码:import json

input = '''{

"text":"Sample data",

"subjects":[

{

"id":"A",

"quant":10

},

{

"id":"B",

"quant":9

},

{

"id":"C",

"quant":8

},

{

"id":"D",

"quant":7

},

{

"id":"E",

"quant":6

}]}

'''

info = json.loads(input)

count = 0

total = 0

for item in info:

value = item["subjects"][0]["quant"]

value = int(value)

total += value

count += count

print 'Count: ', count

print 'Sum: ', total

错误:; exit; {u'text': u'Sample data', u'subjects': [{u'quant': 10, u'id':

u'A'}, {u'quant': 9, u'id': u'B'}, {u'quant': 8, u'id': u'C'},

{u'quant': 7, u'id': u'D'}, {u'quant': 6, u'id': u'E'}]} Traceback

(most recent call last): File

"/Users/macme/Documents/Python/test_Json.py",

line 61, in

value = item["subjects"][0]["quant"] TypeError: string indices must be integers logout Saving session... ...copying shared history...

...saving history...truncating history files... ...completed.

[Process completed]

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值