|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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.
|
|
#2
|
||||
|
||||
|
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? Code:
>>> someTime = 'February 29' >>> someTime[:-3] 'February' >>> splitTime = someTime.split() >>> splitTime[0] 'February' >>> month, day = someTime.split() >>> month 'February' >>> Mark . |
|
#3
|
|||
|
|||
|
Quote:
Only feed it "February 29" in leap years? Code:
>>> 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
|
|
#4
|
|||
|
|||
|
Quote:
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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Problem with time.strptime() |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|