December 8th, 2004, 05:44 PM
Problem with time.strptime()
So I'm trying to use strptime() to strip the day of the month out of a string, but stptime() apparently doesn't like the date "February 29" and gives a Value Error, even though it is clearly (sometimes) valid. Is there any way I can prevent this? Thanks.
December 8th, 2004, 06:09 PM
You have to pass strptime() the format of the time along with time itself; since you didn't post any code I can't tell you this for sure but it seems like the format you're giving could be wrong.
Anyway, with such a simple format why not just use split() or a slice to remove the day?
>>> someTime = 'February 29'
>>> splitTime = someTime.split()
>>> month, day = someTime.split()
December 8th, 2004, 06:12 PM
Only feed it "February 29" in leap years?
>>> time.strptime("2000 February 29", "%Y %B %d")
(2000, 2, 29, 0, 0, 0, 1, 60, -1)
>>> time.strptime("2001 February 29", "%Y %B %d")
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "C:\Python23\lib\_strptime.py", line 513, in strptime
julian = datetime_date(year, month, day).toordinal() - \
ValueError: day is out of range for month
December 8th, 2004, 06:18 PM
Yeah, that's what I just did, and it works. (I wasn't giving it a year to parse.) Seems slightly clumsy, but it works. Thanks.