Python pyowm库平均温度

Python pyowm library average tempature

我有以下代码:

import tkinter as tk
import time
import pyowm

class App():
    def __init__(self):
        self.root = tk.Tk()
        self.root.attributes("-fullscreen", True)
        self.label = tk.Label(text="")
        self.weatherlabel = tk.Label(text="")
        self.label.pack()
        self.update_clock()
        self.root.mainloop()

    def update_clock(self):
        now = time.strftime("%H:%M:%S")
        owm = pyowm.OWM()
        owm = pyowm.OWM('*censored*')
        observation = owm.weather_at_place('Modiin,il')
        test = owm.weather_at_place('Modiin,il').get_weather().get_temperature('celsius')
        print(test)
        self.label.configure(text=now,font=("Helvetica",40),fg="white",bg="black")
        self.weatherlabel.configure(text= test,font=("Helvetica",40),fg="white",bg="black")
        self.root.configure(background='black')
        self.root.after(1000, self.update_clock)
        self.label.place(x=1310,y=10)
        self.weatherlabel.place(x=400,y=400)

app = App()

有没有办法分别请求平均温度、最高温度和最低温度,而不是它给出的巨大字符串?这里的输出是{'temp': 30.48, 'temp_max': 31.0, 'temp_min': 30.0, 'temp_kf': None}

我知道可以使用 test[x] 以字符串方式寻址每个字符,但这并不可靠,除非我添加很多参数来检测每个段中它是一位数字还是两位数字.我想要一个更简单的解决方案。

感谢您的帮助

好像是字典,你试过这样显示结果吗?

dict = {'temp': 30.48, 'temp_max': 31.0, 'temp_min': 30.0, 'temp_kf': None}

print dict['temp']
print "Maximum temperature : {}".format(dict['temp_max'])

输出:

30.48
Maximum temperature : 31.0

如果结果在 test 变量中,您可以这样做:

def update_clock(self):
    now = time.strftime("%H:%M:%S")
    owm = pyowm.OWM()
    owm = pyowm.OWM('*censored*')
    observation = owm.weather_at_place('Modiin,il')
    test = owm.weather_at_place('Modiin,il').get_weather().get_temperature('celsius')
    print "Average temperature : {}".format(test['temp'])
    print "Minimum temperature : {}".format(test['temp_min'])
    print "Maximum temperature : {}".format(test['temp_max'])
    self.label.configure(text=now,font=("Helvetica",40),fg="white",bg="black")
    self.weatherlabel.configure(text= test,font=("Helvetica",40),fg="white",bg="black")
    self.root.configure(background='black')
    self.root.after(1000, self.update_clock)
    self.label.place(x=1310,y=10)
    self.weatherlabel.place(x=400,y=400)