打开天气图 API 没有为 python 请求提供正确的数据
open weather map API not providing right data with python requests
所以,我正在 python 中使用语音识别制作语音助手。我正在使用开放天气图 API。 API 在我的 python 程序中调用时返回错误结果。
当我在网络上使用 link 和给定的 api 键时,API 完美地工作并且 returns 正确的结果但是当我在我的 python 对其进行编程 returns 一些城市的错误结果,如孟买、德里、加尔各答,但当我询问纽约天气时,结果正确。很奇怪!!
查询是我命令的语句 - "What is the weather in New York"
elif "weather" in query:
query = query.strip(" weather in ")
query = query.replace(" ", "")
url = 'http://api.openweathermap.org/data/2.5/weather?q={}&appid=xxxxx&units=metric'.format(query)
res = requests.get(url)
data = res.json()
temp = data['main']['temp']
wind_speed = data['wind']['speed']
description = data['weather'][0]['description']
print(str(description) + " with a temperature of " + str(temp) + "°C and a wind speed of " + str(wind_speed) + " km/hr")
speak(str(description) + " with a temperature of " + str(temp) + "°C and a wind speed of " + str(wind_speed) + " kilometer per hour")
首先,strip
不是这样的。 Strip 从字面上删除由这些字符组成的任何前缀和后缀。它不会剪切或删除给定的部分。
你的情况:
>>> print("what is the weather in new york".strip(" weather in "))
s the weather in new york
另一个例子:
>>> print("aaaaaaaaabsomethinghereaa".strip("a"))
bsomethinghere
>>> print("aaaaaaaaabsomethinghereaa".strip("ab"))
somethinghere
>>> print("1234567aabsomethinghereaa".strip("ab7654321"))
somethinghere
如您所见,顺序无关紧要,这些字符的任意组合都会从头到尾删除。
这给了我们第二步:调试。
如果某些东西没有按预期工作,请检查您是否正确调用它。如果您使用此处的文本,请打印文本操作的每个步骤。您仅将脚本结果与手动结果进行了比较,而没有检查导致这些结果的每个步骤。
如何修复这部分?
你必须切断你的字符串,只留下城市在那里。这意味着你必须找到 "weather in" 的末尾,然后把后面的所有东西都拿走。
要查找子字符串的开头,您可以使用 find
。如果要结束,请加上文字的长度。
>>> query = "what is the weather in new york"
>>> query = query[query.find("weather in")+10:]
>>> query = query.replace(" ", "")
>>> print(query)
newyork
所以,我正在 python 中使用语音识别制作语音助手。我正在使用开放天气图 API。 API 在我的 python 程序中调用时返回错误结果。
当我在网络上使用 link 和给定的 api 键时,API 完美地工作并且 returns 正确的结果但是当我在我的 python 对其进行编程 returns 一些城市的错误结果,如孟买、德里、加尔各答,但当我询问纽约天气时,结果正确。很奇怪!!
查询是我命令的语句 - "What is the weather in New York"
elif "weather" in query:
query = query.strip(" weather in ")
query = query.replace(" ", "")
url = 'http://api.openweathermap.org/data/2.5/weather?q={}&appid=xxxxx&units=metric'.format(query)
res = requests.get(url)
data = res.json()
temp = data['main']['temp']
wind_speed = data['wind']['speed']
description = data['weather'][0]['description']
print(str(description) + " with a temperature of " + str(temp) + "°C and a wind speed of " + str(wind_speed) + " km/hr")
speak(str(description) + " with a temperature of " + str(temp) + "°C and a wind speed of " + str(wind_speed) + " kilometer per hour")
首先,strip
不是这样的。 Strip 从字面上删除由这些字符组成的任何前缀和后缀。它不会剪切或删除给定的部分。
你的情况:
>>> print("what is the weather in new york".strip(" weather in "))
s the weather in new york
另一个例子:
>>> print("aaaaaaaaabsomethinghereaa".strip("a"))
bsomethinghere
>>> print("aaaaaaaaabsomethinghereaa".strip("ab"))
somethinghere
>>> print("1234567aabsomethinghereaa".strip("ab7654321"))
somethinghere
如您所见,顺序无关紧要,这些字符的任意组合都会从头到尾删除。
这给了我们第二步:调试。
如果某些东西没有按预期工作,请检查您是否正确调用它。如果您使用此处的文本,请打印文本操作的每个步骤。您仅将脚本结果与手动结果进行了比较,而没有检查导致这些结果的每个步骤。
如何修复这部分?
你必须切断你的字符串,只留下城市在那里。这意味着你必须找到 "weather in" 的末尾,然后把后面的所有东西都拿走。
要查找子字符串的开头,您可以使用 find
。如果要结束,请加上文字的长度。
>>> query = "what is the weather in new york"
>>> query = query[query.find("weather in")+10:]
>>> query = query.replace(" ", "")
>>> print(query)
newyork