Reputation: 17
I have a problem with time conversion in Python: I try to do this:
from datetime import datetime
date_object = datetime.strptime('12:29:31.181', '%H:%M:%S')
And I receive an error: "ValueError: unconverted data remains: .181"
Can you help me?
Upvotes: 2
Views: 64
Reputation: 394339
You need to add %f
for microseconds:
In [335]:
from datetime import datetime
date_object = datetime.strptime('12:29:31.181', '%H:%M:%S.%f')
print(date_object)
1900-01-01 12:29:31.181000
Your format string has to consume all the characters in the passed in string, if any are still remaining then the ValueError
is raised.
Upvotes: 2