Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unconverted data remains: 15 [closed]

I am trying to generate a random date and the add some days to that date, but I am facing this error. I guess it has to do with the format of my date but I can't find the solution.

I thought it was because I needed double digit numbers for the day and month, here is my code which produces an error.

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")

Any idea on what I am doing wrong?

Thanks

like image 880
JordanBelf Avatar asked Feb 11 '15 14:02

JordanBelf


People also ask

How do you delete unconverted data in Python?

Unless you want to rewrite strptime (a very bad idea), the only real option you have is to slice end_date and chop off the extra characters at the end, assuming that this will give you the correct result you intend. For example: parse_prefix( '2015-10-15 11:33:20.738 45162 INFO core.

How do I use Strptime in Python?

The strptime() function in Python is used to format and return a string representation of date and time. It takes in the date, time, or both as an input, and parses it according to the directives given to it. It raises ValueError if the string cannot be formatted according to the provided directives.


1 Answers

Because %y is for a two-digit year, so 2015 is interpreted as 20 (i.e. 2020) and the 15 is left over:

>>> 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

You want %Y (note case!), which is a four-digit year:

>>> datetime.datetime.strptime("01/02/2015", "%d/%m/%Y")
datetime.datetime(2015, 2, 1, 0, 0)

You should read through the docs, which explain the various format directives.


However, the extra steps involving strings seem pointless, why not just pass the integers you create to 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)

Note that this might generate invalid dates (e.g. February doesn't have a 30th!):

>>> 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
like image 72
jonrsharpe Avatar answered Sep 28 '22 21:09

jonrsharpe