Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unconverted data remains: .387000 in Python

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.

like image 933
TheBigOnion Avatar asked Mar 23 '15 17:03

TheBigOnion


2 Answers

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
like image 110
Selcuk Avatar answered Sep 27 '22 21:09

Selcuk


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
like image 26
Mauro Baraldi Avatar answered Sep 27 '22 21:09

Mauro Baraldi