【发布时间】:2015-10-01 23:12:27
【问题描述】:
我编写了一个代码,它从用户那里获取start_date 和end_date,但它抛出了一个错误。
以下是代码:
from datetime import datetime
start_date = datetime.strptime(input('Enter Start date in the format m/d/y'), '%m/%d/%Y')
end_date = datetime.strptime(input('Enter End date in the format m/d/y'), '%m/%d/%Y')
dates=['4/6/2013', '5/4/2013', '6/26/2013', '7/26/2013', '9/5/2013', '10/7/2013', '10/12/2013', '4/12/2014', '5/10/2014', '6/12/2014', '7/19/2014', '8/15/2014', '9/17/2014', '4/21/2015', '5/28/2015', '6/26/2015']
# this line creates a list of datetime objects from the given strings in list dates
dt_dates = [datetime.strptime(date, '%m/%d/%Y') for date in dates]
in_between_dates = []
for d in dt_dates:
if d >= start_date and d <= end_date:
in_between_dates.append(d)
print [d.strftime('%m/%d/%Y') for d in in_between_dates]
输入1/1/2014后弹出错误如下:
Enter Start date in the format m/d/y1/1/2014
Traceback (most recent call last):
File "C:\Users\Ayush\Desktop\UWW Data Examples\new csvs\temp.py", line 9, in <module>
start_date = datetime.strptime(input('Enter Start date in the format m/d/y'), '%m/%d/%Y')
TypeError: must be string, not int
【问题讨论】:
-
你能把例子简化为make it as small as possible吗?
标签: python list datetime input types