首页 > 解决方案 > 如何返回星期几,然后从 Weather API 返回星期的名称?

问题描述

def getForecast():

    zip2 = '60103'

    url = 'http://api.openweathermap.org/data/2.5/forecast?zip=' \
      '' + zip2 + 
         ',us&appid=7f5941b864a5fde449419c6aaca23540&units=imperial'.format(zip2)

    response = requests.get(url)
    data2 = response.json()  

    date_list = (data2['list'][0]['dt_txt'])
    date_list = date_list.split()
    del date_list[-1]
    new = list(date_list[0].split('-'))
    new1 = [int(x) for x in new]


    print(calendar.weekday(new1))

根据日历文档,“new1”应该以正确的格式返回星期几,但我收到一个错误:“ weekday() missing 2 required positional arguments: 'month' and 'day'”当我手动插入年、月、日时,我收到的星期几只是美好的。

标签: pythonweather-api

解决方案


docs,您需要提供 3 个参数year, month, day才能weekday运行,但是您给了 a list,您new1的类型list是这样 的

import calendar

# if you have a list with year, month, day
# for example this
new1 = [2018, 6, 19]

print(calendar.weekday(*new1)) # important to put * before new1

# output
# 1

推荐阅读