Python datetime.strptime: ignore part of a second

I am working on converting human read time into an object datetime. For this I use datetime.datetime.strptime.

However, the simple enough reading time by a person that I have contains fractions of a second that I cannot make out. If it were a constant, I could include it as part of the format. However, since it is not permanent, I cannot do this.

Here is what I am doing right now:

>>> humanTime = '2012/06/10T16:36:20.509Z'
>>> datetime.datetime.strptime(humanTime, "%Y/%m/%dT%H:%M:%SZ")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "lib/python2.7/_strptime.py", line 325, in _strptime
    (data_string, format))
ValueError: time data '2012-06-10T16:36:20.507Z' does not match format '%Y-%m-%dT%H:%M:%SZ'

So, I think the problem here is that the split second is not processed. I don't give a damn about this split second. If you don’t cut the line, is there a way I can ask to datetimeignore the split second (preferably in the format)?

, - . .

+5
2

, 19 , , .

>>> humanTime = '2012/06/10T16:36:20.509Z'
>>> datetime.datetime.strptime(humanTime[:19], "%Y/%m/%dT%H:%M:%S")
datetime.datetime(2012, 6, 10, 16, 36, 20)
+4

: % f :

>>> datetime.datetime.strptime(humanTime, "%Y/%m/%dT%H:%M:%S.%fZ")

. 6 , .

, , , datetime.datetime.strptime . - datetime, datetime.timedelta.

>>> import numpy as np
>>> import datetime
>>> 
>>> humanTime = '2012/06/10T16:36:20.509Z'
>>> dt_time_no_seconds = datetime.datetime.strptime(humanTime[:-8], "%Y/%m/%dT%H:%M")
>>> seconds = np.float(humanTime[-7:-1])
>>> dt_time = dt_time_no_seconds+datetime.timedelta(seconds=seconds)
>>> dt_time_no_seconds
datetime.datetime(2012, 6, 10, 16, 36)
>>> dt_time
datetime.datetime(2012, 6, 10, 16, 36, 20, 509000)

, .

+3

All Articles