拆分字符串不可用类型

时间:2016-10-15 22:27:55

标签: python python-3.x

我以前从未在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

1 个答案:

答案 0 :(得分:2)

get_temperature返回一个字典,然后您尝试使用slice对象进行索引,该对象不可清除。 e.g。

>>> hash(slice(5, 10))                                                                         
Traceback (most recent call last):                                                             
  File "<stdin>", line 1, in <module>                                                          
TypeError: unhashable type

要获得温度,你需要从字典中得到它:

temperature['temp']
相关问题