python datetime.strptime: ignore fraction of a second

datetime, floating-point, format, python-2.7

Solution

Since I really don't care about the fractions of a second, I should just take the first `19` characters of the human readable string, and apply a simple format on that.

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

Problem

I'm working on converting a human readable time to a `datetime` object . In order to do this, I'm using `datetime.datetime.strptime`. Simple enough, however, the human readable time that I have contains fractions of a second, which I'm not able to parse. If this was a constant, I could incorporate it as part of the format. However, since it is not a constant, I am unable to do so. This is what I'm 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 figure that the issue here is that the fraction of a second is not parseable. I don't really care about that fraction of a second. Short of slicing the string, is there a way by which I can ask `datetime` to ignore the fraction of a second (preferably with the format)? I have a feeling that I might be missing something very basic. I'd appreciate any help.

Original source