在python中将struct_time转换为String时出错

时间:2018-08-23 07:36:46

标签: python-3.x time argparse strptime strftime

我使用以下代码运行my.py 20180822,将struct_time转换为字符串

def mkdate(datestr):
    return time.strptime(datestr, '%Y%m%d')

parser = argparse.ArgumentParser(description='Download Images from Web Site')
parser.add_argument('startDate',type=mkdate, nargs='*', help='Start Date to Download')

    if args.startDate:
        startDate = time.strftime('%Y%m%d', args.startDate)

我收到如下错误,尽管我没有任何答案就用谷歌搜索怎么办...

    [time.struct_time(tm_year=2018, tm_mon=8, tm_mday=22, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=234, tm_isdst=-1)]
2018/08/23 16:21:28 ERROR    Exception: Traceback (most recent call last):
  File "C:\My.py", line 8, in main
    startDate = time.strftime('%Y%m%d', args.startDate)
TypeError: Tuple or struct_time argument required

1 个答案:

答案 0 :(得分:0)

我有需要的东西,是

startDate = time.strftime('%Y%m%d', args.startDate[0])