从 Open Weather Map API get_temperature() string in Python 中提取数字

Extracting number from Open Weather Map API get_temperature() string in Python

我正在做一个项目,我希望我的 RPi 在使用 PIR 传感器检测到运动后打开 LED 并播放歌曲。这是为了模拟一个“智能家居”系统。将播放的歌曲类型将取决于外部温度。

为了将外部温度输入我的程序,我使用了免费的 Open Weath Map API,问题是用于 return 此数据的函数 get_temperature() 函数, returns 以下字符串:

weather.get_temperature('celsius') #returns: {'temp_max': 10.5, 'temp': 9.7, 'temp_min': 9.0} 

我想提取 'temp': 之后的部分,在这种情况下是 9.7 因为我想使用这个值来确定我要播放的歌曲。

希望你们中有人知道如何完成这项工作,因为这是完成我的程序所需要的最后一件事!

提前致谢, 工作

get_temperature returns 带有温度信息的 dict。您可以从给定键的字典中获取值。

temperature = weather.get_temperature('celsius')
temp_value = temperature.get('temp')