user3142570
user3142570

Reputation: 17

Conversion time from string

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

Answers (1)

EdChum
EdChum

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

Related Questions