caption_text=u'{"confidence_score": 0.847656, "sentence_type": "Abusive"}'
for i in caption_text:
if i['sentence_type']=='Abusive':
print i['confidence_score']
获得
TypeError: string indices must be integers
答案 0 :(得分:0)
您尚未解码JSON数据,因此您循环遍历字符串。
使用json
module和不要循环解码数据;只有一个字典,而不是列表:
import json
caption_text=u'{"confidence_score": 0.847656, "sentence_type": "Abusive"}'
data = json.loads(caption_text)
if data['sentence_type'] == 'Abusive':
print data['confidence_score']