我以前从未在 Python 中拆分过字符串,所以我不太确定这里出了什么问题。
import pyowm
owm = pyowm.OWM('####################')
location = owm.weather_at_place('Leicester, uk')
weather = location.get_weather()
weather.get_temperature('celsius')
temperature = weather.get_temperature('celsius')
print(temperature[5:10])
收到错误
sudo python weather.py
Traceback (most recent call last):
File "weather.py", line 10, in <module>
print(temperature[5:10])
TypeError: unhashable type
最佳答案
get_Temperature
返回一个字典,然后您尝试使用不可散列的 slice
对象对其进行索引。例如
>>> hash(slice(5, 10))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type
要获取温度,您需要像这样从字典中获取:
temperature['temp']
关于python - 分割不可散列类型的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40064881/