使用 Python...
Using Python...
如何获取特定一周中的天数列表?
How can I get a list of the days in a specific week?
类似...
{
'1' : ['01/03/2010','01/04/2010','01/05/2010','01/06/2010','01/07/2010','01/08/2010','01/09/2010'],
'2' : ['01/10/2010','01/11/2010','01/12/2010','01/13/2010','01/14/2010','01/15/2010','01/16/2010']
}
本例中字典的键是周数.
The key of the dictionary in this example would be the week number.
小心!如果要定义自己的周数,可以使用提供的生成器表达式 在你的第一个问题中,顺便说一句,得到了一个很棒的答案).如果您想遵循 ISO 对周数的约定,则需要小心:
Beware! If you want to define YOUR OWN week numbers, you could use the generator expression provided in your first question which, by the way, got an awesome answer). If you want to follow the ISO convention for week numbers, you need to be careful:
一年的第一个日历周是包括第一个的那个那年的星期四和 [...]一个日历年的最后一个日历周是紧接前一周下一个日历周的第一个日历周日历年.
the first calendar week of a year is that one which includes the first Thursday of that year and [...] the last calendar week of a calendar year is the week immediately preceding the first calendar week of the next calendar year.
例如,2010 年 1 月 1 日和 2 日不是 2010 年的第一周,而是 2009 年的第 53 周.
So, for instance, January 1st and 2nd in 2010 were NOT week one of 2010, but week 53 of 2009.
Python 提供了一个使用 ISO 日历查找周数的模块:
Python offers a module for finding the week number using the ISO calendar:
示例代码:
h[1] >>> import datetime
h[1] >>> Jan1st = datetime.date(2010,1,1)
h[1] >>> Year,WeekNum,DOW = Jan1st.isocalendar() # DOW = day of week
h[1] >>> print Year,WeekNum,DOW
2009 53 5
再次注意,2010 年 1 月 1 日对应于 2009 年第 53 周.
Notice, again, how January 1st 2010 corresponds to week 53 of 2009.
使用上一个答案中提供的生成器:
Using the generator provided in the previous answer:
from datetime import date, timedelta
def allsundays(year):
"""This code was provided in the previous answer! It's not mine!"""
d = date(year, 1, 1) # January 1st
d += timedelta(days = 6 - d.weekday()) # First Sunday
while d.year == year:
yield d
d += timedelta(days = 7)
Dict = {}
for wn,d in enumerate(allsundays(2010)):
# This is my only contribution!
Dict[wn+1] = [(d + timedelta(days=k)).isoformat() for k in range(0,7) ]
print Dict
Dict 包含您请求的字典.
Dict contains the dictionary you request.
这篇关于如何使用 Python 获取当前周?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!