我發現了一個「TypError」,我不知道如何解決它。請幫忙。如果可能,我會非常感激的解釋。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]
抱歉關於錯誤框格式。 – RBB