Sur*_*esh 5 python datetime scrapy python-3.x python-datetime
我正在尝试使用python获取上周的日期。
如果日期为:2014年10月10日为
应该打印
10 OCT 2014, 09 OCT 2014, 08 OCT 2014, 07 OCT 2014, 06 OCT 2014, 05 OCT 2014, 04 OCT 2014
Run Code Online (Sandbox Code Playgroud)
我试过了:
today = (10 OCT 2014)
dates = [today + datetime.timedelta(days=i) for i in range(-4 - today.weekday(), 4 - today.weekday())]
print dates
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
exceptions.AttributeError: 'unicode' object has no attribute 'weekday'
Run Code Online (Sandbox Code Playgroud)
您的问题和算法似乎并不对应。这是你所追求的吗?
from datetime import date
from datetime import timedelta
today = date(2014, 10, 10) # or you can do today = date.today() for today's date
for i in range(0,7):
print (today - timedelta(days=i))
Run Code Online (Sandbox Code Playgroud)