使用python中的数据范围不正确的语法错误

时间:2014-07-30 09:42:33

标签: python

我一直在研究这个脚本我需要知道如何使用日期范围,如果想查看上周的数据我可以使用dl_date = datetime.date.today() - 5days。它给了我不正确的语法错误。

    try:
    site_id = sys.argv[1]
    try:
        dl_date = sys.argv[2]
    except:
        oneday = datetime.timedelta(days=1)
        dl_date = datetime.date.today() - oneday
except:
    print "Usage: %s site_id [date]"
    sys.exit(1)

# Process the cameras for that site
for cam_id in cam_id_list:
    cam_name = db_obj.cam_name_by_id(cam_id)
    print "Downloading %s for %s" % (cam_name, dl_date)
    try:
        #myftp = anprint.ftpClient(cam_name)
    rsync = anprint.ftpClient(cam_name)
        #dl_count = myftp.downloaddir(dl_date)
        dl_count = rsync.downloaddir(dl_date)
    db_obj.log_stats(cam_id, dl_count, dl_date)

        fp = open(stats_filename, "a")
        stats_string = "Downloaded %d entries from %s\n" % (dl_count, cam_name)
        fp.write(stats_string)
        fp.close()
    except:
        pass

2 个答案:

答案 0 :(得分:0)

dateutil包将非常有用

import dateutil.relativedelta
from datetime import datetime

now = datetime.now()
print now + dateutil.relativedelta.relativedelta(weeks=-1)

答案 1 :(得分:0)

使用timedelata

from datetime import date, timedelta
dl_date  = date.today() - timedelta(days=5)