我如何得到当前星期使用Python? [英] How can I get the current week using Python?

查看:207
本文介绍了我如何得到当前星期使用Python?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Python ...



如何获取特定周内的日期列表?



有人喜欢...

  {
'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']
}

解决方案

注意!如果你想定义你自己的周数,你可以使用生成表达式,顺便说一句,得到一个真棒的答案)。如果您想遵循ISO编号的周数,您需要小心:


一年的第一个日历周是
包括该年的第一个
星期四和[...]
一个日历年的最后一个日历周
是紧接
第一个日历之前的一周


因此,例如 2010年1月1日和2月



Python提供了使用ISO日历查找周数的模块:



示例代码:

  h [1]> import datetime 
h [1]>>>> Jan1st = datetime.date(2010,1,1)
h [1]>>> Year,WeekNum,DOW = Jan1st.isocalendar()#DOW = week of week
h [1]>>> print Year,WeekNum,DOW
2009 53 5

再次注意,2010年1月1日

 

使用上一个答案中提供的生成器

code>从datetime导入日期,timedelta


def allsundays(年):
这个代码是在上一个答案提供的!
d = date(year,1,1)#January 1st
d + = timedelta(days = 6 - d.weekday())#第一个星期天
while d.year == year :
yield d
d + = timedelta(days = 7)

Dict = {}
for wn,d in enumerate(allsundays(2010)):
#这是我唯一的贡献!
Dict [wn + 1] = [(d + timedelta(days = k))。在范围(0,7)中k的isoformat()]

print Dict

Dict包含您请求的字典。


Using Python...

How can I get a list of the days in a specific week?

Something like...

{
'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.

解决方案

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.

So, for instance, January 1st and 2nd in 2010 were NOT week one of 2010, but week 53 of 2009.

Python offers a module for finding the week number using the ISO calendar:

Example code:

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

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 contains the dictionary you request.

这篇关于我如何得到当前星期使用Python?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆