unconverted data remains: .387000 in Python
Asked Answered
S

5

14

I have a datetime that is a string I read from a text file. I want to trim the extra milliseconds off of it, but I want to convert it to a datetime variable first. This is because it may be in different formats depending on what is in the text file. How ever, everything I have tried wants me to already know the format. Please take a look at what I am trying here:

import time, datetime

mytime = "2015-02-16 10:36:41.387000"
myTime = time.strptime(mytime, "%Y-%m-%d %H:%M:%S.%f")

myFormat = "%Y-%m-%d %H:%M:%S"

print datetime.datetime.fromtimestamp(time.mktime(time.strptime(mytime, myFormat)))

But this gives me this error:

File "python", line 10, in <module>
ValueError: unconverted data remains: .387000`

Can someone please tell me how to do a normal datetime format? In other languages I can pass in various formats and then set it to a new format without a problem.

Steiger answered 23/3, 2015 at 17:11 Comment(0)
B
31

You are doing it backwards. Try this:

from datetime import datetime

mytime = "2015-02-16 10:36:41.387000"
myTime = datetime.strptime(mytime, "%Y-%m-%d %H:%M:%S.%f")

myFormat = "%Y-%m-%d %H:%M:%S"

print "Original", myTime
print "New", myTime.strftime(myFormat)

result:

Original 2015-02-16 10:36:41.387000
New 2015-02-16 10:36:41
Bernadettebernadina answered 23/3, 2015 at 17:16 Comment(0)
K
7

You forgot to reference microseconds in myFormat

myFormat = "%Y-%m-%d %H:%M:%S.%f"

Anyway, you can convert it with less steps

from datetime import datetime

mytime = "2015-02-16 10:36:41.387000"
full = "%Y-%m-%d %H:%M:%S.%f"
myTime = datetime.strptime(mytime, full)
>>> datetime.datetime(2015, 2, 16, 10, 36, 41, 387000)

Here mytime is in datetime object. If you want print without microseconds, use the strftime

myfmt = "%Y-%m-%d %H:%M:%S"
print datetime.strptime(mytime, full).strftime(myfmt)
>>> 2015-02-16 10:36:41
Koumis answered 23/3, 2015 at 17:16 Comment(1)
That actually creates a string not a datetime object which is what the OP's code is trying to doMicrodot
T
0

alternative solution is using split():

# mytime is str(timestamp)
datetime.datetime.strptime(mytime.split('.')[0], '%Y-%m-%d %H:%M:%S')
Talaria answered 21/5, 2020 at 8:35 Comment(0)
C
0
mytime = "2015-02-16 10:36:41.387000 "

myTime = time.strptime(mytime, "%Y-%m-%d %H:%M:%S.%f")

->

ValueError: unconverted data remains:

it is my case, and what I need is mytime.strip()

Cervin answered 18/3 at 7:28 Comment(0)
F
-1

If nothing else is changing, i.e. the format, except that you are dropping the micro seconds at the end, you don't really need to call strftime. You can just substring it:

mytime = "2015-02-16 10:36:41.387000"
mytime = mytime[:-7]    
print mytime

result:

2015-02-16 10:36:41

Disclaimer: This is only be applicable if you do not to re-format or rearrange anything, and only if you are trying to drop the last bit.

Flank answered 30/11, 2016 at 22:31 Comment(1)
its too crude for me (11M rows of data), but this is the only answer for ignoring dataKellby

© 2022 - 2024 — McMap. All rights reserved.