【发布时间】:2015-02-12 13:22:17
【问题描述】:
我正在尝试使用 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
我试过了:
today = (10 OCT 2014)
dates = [today + datetime.timedelta(days=i) for i in range(-4 - today.weekday(), 4 - today.weekday())]
print dates
我收到此错误:
exceptions.AttributeError: 'unicode' object has no attribute 'weekday'
【问题讨论】:
-
我无法在 Python 2.7 中重现,你能说明你是如何初始化你的
today变量的吗?您是否使用datetime对象像这样datetime.date(2014, 10, 10)创建它?错误消息似乎表明您不是。 -
今天 = datetime.date.today()
标签: python datetime python-3.x scrapy python-datetime