0

This question is very similar to this SO, but my data is on the form:

'13-04-2018 14:06:26.866'
'13-04-2018 14:06:27.131'
'13-04-2018 14:06:27.404'
'13-04-2018 14:06:27.674'
...

i.e. the seconds are given as decimals. My reading of the datetime documentation suggests that it doesn't support this format, so I am not sure how to best proceed.

Toke Faurby
  • 4,666
  • 8
  • 32
  • 56
  • The question you linked in your question described perfectly the two parameters you need to convert your strings to time. – user3483203 May 08 '18 at 15:03
  • @chrisz I don't see it. What parameters are used to read seconds as floats? – Toke Faurby May 08 '18 at 15:05
  • @TokeFaurby You use `%S` for the integer part of the seconds and `%f` for the fractional part of the seconds. There is no format specifier for "seconds with fractions" or "seconds as floats". – Alfe May 08 '18 at 15:07
  • 1
    @Alfe https://stackoverflow.com/questions/698223/how-can-i-parse-a-time-string-containing-milliseconds-in-it-with-python better dupe target? – user3483203 May 08 '18 at 15:09
  • @chrisz Yup, way better :) – Alfe May 08 '18 at 15:10

1 Answers1

2

Looks like you need

  • .%f == Microsecond as a decimal number, zero-padded on the left.

Ex:

import datetime
s = '13-04-2018 14:06:26.866'
print(datetime.datetime.strptime(s, "%d-%m-%Y %H:%M:%S.%f"))

Output:

2018-04-13 14:06:26.866000
Rakesh
  • 75,210
  • 17
  • 57
  • 95