使用pywapi获取用户输入的位置ID
问题描述:
我想制作一个简单的CLI应用程序来检索天气数据。不幸的是,我对此没有太大的了解,因为我在早期阶段就陷入困境。这是到目前为止我的代码:使用pywapi获取用户输入的位置ID
import pywapi, string
loc=input("What is the city you're closest to?")
loc=loc.lower
#this will give you a dictionary of all cities in the world with this city's name Be specific (city, country)!
loc_id=pywapi.get_location_ids(loc)
#apparently this is a needed workaround to access last item of dictionary
for i in loc_id:
loc_id=i
#before I go on to code anything further, I just want to use print to check that I've got the two variables I need
print (loc,loc_id)
当被问及对于一个城市,我可以进入伦敦例如,伦敦,英国,但都扔了一个错误:(这是我的本地机器上)
Traceback (most recent call last):
File "get_weather.py", line 7, in <module>
loc_id=pywapi.get_location_ids(loc)
File "/home/james/.local/lib/python3.4/site-packages/pywapi.py", line 825, in get_location_ids
loc_id_data = get_loc_id_from_weather_com(search_string)
File "/home/james/.local/lib/python3.4/site-packages/pywapi.py", line 856, in get_loc_id_from_weather_com
url = LOCID_SEARCH_URL % quote(search_string)
File "/usr/lib/python3.4/urllib/parse.py", line 694, in quote
return quote_from_bytes(string, safe)
File "/usr/lib/python3.4/urllib/parse.py", line 719, in quote_from_bytes
raise TypeError("quote_from_bytes() expected bytes")
TypeError: quote_from_bytes() expected bytes
而这个错误,这是不同的,当我使用Pythonanywhere
Traceback (most recent call last):
File "/home/pydavith/get_weather.py", line 7, in <module>
loc_id=pywapi.get_location_ids(loc)
File "/home/pydavith/.local/lib/python3.6/site-packages/pywapi.py", line 825, in get_location_ids
loc_id_data = get_loc_id_from_weather_com(search_string)
File "/home/pydavith/.local/lib/python3.6/site-packages/pywapi.py", line 852, in get_loc_id_from_weather_co
m
search_string = unidecode(search_string)
File "/usr/local/lib/python3.6/dist-packages/unidecode/__init__.py", line 48, in unidecode_expect_ascii
bytestring = string.encode('ASCII')
AttributeError: 'builtin_function_or_method' object has no attribute 'encode'
没有任何人有任何想法是怎么回事错在这里?我已经广泛地用鸭子和谷歌搜索,但没有发现任何东西。帮助将不胜感激!
答
好的,我自己已经找到了一个可行的答案。我在这里分享代码,以防万一这有助于其他人。
我在这里改变了很多东西,很难知道从哪里开始。也就是说,我最初发布的代码有很多错误和需要改进的地方。
import pywapi
# getting input from the user regarding location
#this will give a dictionary of all cities in the world with this city's name
loc=str.title(input("What is the city you're closest to? "))
print("Searching for ", loc)
loc_id=pywapi.get_location_ids(loc)
#Now we have a dictionary (it seems as standard it returns a maximum of ten key:value pairs. Now to convert it to a list just so I can number the entries and ultimately allow the user select the correct entry easily. (Note, there may be a way to do this without converting the dictionary to a list, but I couldn't find it)
loc_list = [ [k,v] for k, v in loc_id.items() ]
#Now I am going to check if the length of the list is over 1 value (i.e. if there's more than one choice, and if there is, to run a for loop printing each entry with a number
list_leng=len(loc_list)
if list_leng >1:
#fix some value for x and y to be used in the for loop
x=1
y = 0
for l in loc_id:
print (x,loc_list[y])
x = x+1
y=y+1
index_choice=(int(input("Which city do you mean? Enter a choice from 1 to " + str(list_leng) + ": ")))-1
else:
index_choice=0
loc_id=loc_list[index_choice]
print("You selected ",loc_id[1])
loc_id=loc_id[0]
print ("Location ID for "+loc+" is ",loc_id)
def gettt_weather(loc_id):
"""Fetches the weather information from Weather.com using a location stored under the variable loc_id"""
print("Looking up the weather for "+loc)
weather_com_result = pywapi.get_weather_from_weather_com(loc_id,units='metric')
print ("Weather.com says that it is "+ weather_com_result['current_conditions']['text'].lower()+" and", weather_com_result['current_conditions']['temperature']+"°C now in " + loc)
gettt_weather(loc_id)
无论如何,这工作正如我想要的,所以我希望这对某些初学者有用。如果有人有任何进一步的改进,请让我知道。
你正在使用python(3.4和3.6)的两个不同版本,所以这可能解释为什么两者不同,如果不是错误本身...... – hwjp