我想循环遍历年份列表并计算每年按日期变化的国家法定假日。我遇到过一些可以用于此目的的库,但我未能在for循环中实现它们。
from datetime import date
from dateutil.easter import easter
from dateutil.relativedelta import relativedelta as rd
#range of years I want to compute this date for
year = list(range(2008,2019))
for i in year:
e = easter(year)
maundy_thursday = e - rd(days=3)
错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-150-1691b0469173> in <module>
2
3 for i in year:
----> 4 e = easter(year)
5 maundy_thursday = e - rd(days=3)
6 good_friday = e - rd(days=2)
C:ProgramDataAnaconda3libsite-packagesdateutileaster.py in easter(year, method)
64
65 y = year
---> 66 g = y % 19
67 e = 0
68 if method < 3:
TypeError: unsupported operand type(s) for %: 'list' and 'int'
看起来你在变量之间混淆了,最终是由误导性的名称引起的。
你有:
year = list(range(2008,2019))
for i in year:
e = easter(year)
注意,您将year
变量传递给easter()
函数,但这实际上是年份列表。
你的意思是:
years = list(range(2008,2019))
for year in years:
e = easter(year)
作为旁注,这里不需要list()
;你可以写:years = range(2008, 2019)