-1

In python while trying to parse time using strptime. I am getting the error unconverted data remains. Can someone please help identify where I am making mistake

>>> from datetime import datetime
>>> s = "00:00:49.9342733"
>>> duration = datetime.strptime(s, '%H:%M:%S.%f')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_strptime.py", line 568, in _strptime_datetime
    tt, fraction, gmtoff_fraction = _strptime(data_string, format)
  File "_strptime.py", line 352, in _strptime
    raise ValueError("unconverted data remains: %s" %
ValueError: unconverted data remains: 3
Jannat Arora
  • 2,759
  • 8
  • 44
  • 70
  • It looks like it's trying to parse as microseconds, but you actually have 7 digits after the decimal place (1 more than needed) – rv.kvetch Oct 27 '21 at 00:28
  • 1
    The `%f` part expects exactly 6 digits, because it corresponds to microseconds. This is made clear by [the documentation](https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes), which clearly shows six-digit example values. And yes, `strptime` does not allow for extra text after the date it's parsing (as would be clear from just about any relevant web search). Sorry to say the solutions for this are hack workarounds. – Karl Knechtel Oct 27 '21 at 00:29

1 Answers1

1

According to Python docs, the %f formatting should contains 6 digits (000000-999999).

To fix this, just slice your original string:

s = "00:00:49.9342733"[:-1]
enzo
  • 9,861
  • 3
  • 15
  • 38