字符串索引必须是整数
我遇到了以下错误:
字符串的索引必须是整数,而不是字符串
我该怎么解决这个问题呢?这是我的代码:
if args['params']['text'][:5] == '!temp':
degreeChar = u'\u00b0'
url = 'http://api.worldweatheronline.com/free/v1/weather.ashx?q=' + args['params']['text'][9:] + '&format=json&num_of_days=1&key=' + WeatherAPIkey
json_obj = urllib.urlopen(url)
data = json.load(json_obj)
temp = int(data['data']['current_condition']['temp_C'])
ws.send('5:::' + genMessage('chatMsg', channel, username, 'Temperature in ' + args['params']['text'][6:] + ': ' + temp + degreeChar + 'C'))
错误出现在这一行:temp = int(data['data']['current_condition']['temp_C'])
这个API在这里:img
1 个回答
1
data['data']['current_condition']
返回的是一个列表,所以你需要使用:
`data['data']['current_condition'][0]['temp_C']`
来访问列表里面的字典。