嗨,我对python很陌生。我试着用rasa做一个聊天机器人供个人使用。现在想添加天气api。我使用weatherstack.com。如果我使用他们的例子,这是可行的,但如果我根据自己的喜好调整它,我总是以同样的错误告终
condition = api_response['current']['weather_descriptions'] TypeError: list indices must be integers or slices, not str
这是我正在使用的代码
class Actionweatherapi(Action):
def name(self):
return "actions.weatherapi"
def run(self, dispatcher, tracker, domain):
import requests
location = tracker.get_slot('location')
if location == 'None':
location = 'fetch:ip'
api_result = requests.get('http://api.weatherstack.com/current?access_key=0000000&query={}'.format(location))
api_response = api_result.json()
country = api_response['request']['query']
condition = api_response['current']['weather_descriptions']
temperature_c = api_response['current']['temperature']
humidity = api_response['current']['humidity']
wind_mph = api_response['current']['wind_speed']
response = """It is currently {} in {} at the moment. The temperature is {} degrees, the humidity is {}% and the wind speed is {} mph.""".format(condition, country, temperature_c, humidity, wind_mph)
dispatcher.utter_message(response)
return [AllSlotsReset()]
我还回顾了python网站上的printf样式的字符串格式,但我不知道如何让它工作
json响应类似于此示例 http://api.weatherstack.com/current?access_key=1c80e8b9fe4fcef4f8d6fd7514a8e9e9&query=New%20York
目前没有回答
相关问题 更多 >
编程相关推荐