未转换的数据仍然存在:15
unconverted data remains: 15
我正在尝试生成一个随机日期并在该日期上添加一些天数,但我遇到了这个错误。我想这与我的日期格式有关,但我找不到解决方案。
我以为是因为我需要双位数的日期和月份,这是我的代码,它产生了错误。
start_day = randint(1,31)
strt_day = []
strt_day.append("%02d" % start_day)
start_day = strt_day
strt_moth = []
start_month = randint(2,4)
strt_moth.append("%02d" % start_month)
start_month = strt_moth
start_date = ""+start_month[0]+"/"+start_day[0]+"/2015"
depart = datetime.datetime.strptime(start_date, "%m/%d/%y")
知道我做错了什么吗?
谢谢
因为 %y
代表 两位数年份 ,所以 2015
被解释为 20
(即 2020
) 并且 15
剩下:
>>> import datetime
>>> datetime.datetime.strptime("01/02/20", "%d/%m/%y")
datetime.datetime(2020, 2, 1, 0, 0)
>>> datetime.datetime.strptime("01/02/2015", "%d/%m/%y")
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
datetime.datetime.strptime("01/02/2015", "%d/%m/%y")
File "C:\Python27\lib\_strptime.py", line 328, in _strptime
data_string[found.end():])
ValueError: unconverted data remains: 15
你想要%Y
(注意大小写!),这是一个四位数的年份:
>>> datetime.datetime.strptime("01/02/2015", "%d/%m/%Y")
datetime.datetime(2015, 2, 1, 0, 0)
您应该通读 the docs,其中解释了各种格式指令。
然而,涉及字符串的额外步骤似乎毫无意义,为什么不直接将您创建的整数传递给 datetime.datetime
?
>>> import random
>>> random.seed(0)
>>> datetime.datetime(2015, random.randint(2, 4), random.randint(1, 31))
datetime.datetime(2015, 4, 24, 0, 0)
请注意,这可能会生成无效日期(例如,2 月没有 30 日!):
>>> random.seed(8)
>>> datetime.datetime(2015, random.randint(2, 4), random.randint(1, 31))
Traceback (most recent call last):
File "<pyshell#28>", line 1, in <module>
datetime.datetime(2015, random.randint(2, 4), random.randint(1, 31))
ValueError: day is out of range for month
我正在尝试生成一个随机日期并在该日期上添加一些天数,但我遇到了这个错误。我想这与我的日期格式有关,但我找不到解决方案。
我以为是因为我需要双位数的日期和月份,这是我的代码,它产生了错误。
start_day = randint(1,31)
strt_day = []
strt_day.append("%02d" % start_day)
start_day = strt_day
strt_moth = []
start_month = randint(2,4)
strt_moth.append("%02d" % start_month)
start_month = strt_moth
start_date = ""+start_month[0]+"/"+start_day[0]+"/2015"
depart = datetime.datetime.strptime(start_date, "%m/%d/%y")
知道我做错了什么吗?
谢谢
因为 %y
代表 两位数年份 ,所以 2015
被解释为 20
(即 2020
) 并且 15
剩下:
>>> import datetime
>>> datetime.datetime.strptime("01/02/20", "%d/%m/%y")
datetime.datetime(2020, 2, 1, 0, 0)
>>> datetime.datetime.strptime("01/02/2015", "%d/%m/%y")
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
datetime.datetime.strptime("01/02/2015", "%d/%m/%y")
File "C:\Python27\lib\_strptime.py", line 328, in _strptime
data_string[found.end():])
ValueError: unconverted data remains: 15
你想要%Y
(注意大小写!),这是一个四位数的年份:
>>> datetime.datetime.strptime("01/02/2015", "%d/%m/%Y")
datetime.datetime(2015, 2, 1, 0, 0)
您应该通读 the docs,其中解释了各种格式指令。
然而,涉及字符串的额外步骤似乎毫无意义,为什么不直接将您创建的整数传递给 datetime.datetime
?
>>> import random
>>> random.seed(0)
>>> datetime.datetime(2015, random.randint(2, 4), random.randint(1, 31))
datetime.datetime(2015, 4, 24, 0, 0)
请注意,这可能会生成无效日期(例如,2 月没有 30 日!):
>>> random.seed(8)
>>> datetime.datetime(2015, random.randint(2, 4), random.randint(1, 31))
Traceback (most recent call last):
File "<pyshell#28>", line 1, in <module>
datetime.datetime(2015, random.randint(2, 4), random.randint(1, 31))
ValueError: day is out of range for month