Sahil
Sahil

Reputation: 439

converting time into seconds+mili seconds

I have a .csv file which contains a column "Time" which comprises of data in the format "01/02/2016 08:01:29;12", So now I want to separate this into two columns. Can anyone suggest me how to do that in python.

I am successfull in splitting the columns, but now i want to convert the time part(08:01:29;12) into seconds. I have written a function but it seems its not working, please help!

def time_convert(x): times = x.split(':') time = x.split(";") return (60*int(times[0])+60*int(times[1]))+int(times[2])+int(time[3])

Upvotes: 0

Views: 90

Answers (1)

vlado
vlado

Reputation: 9

Assuming that ";12" in your date are microseconds, and that you want to separate date and time into two columns (if I understood correctly) it would be the best to convert str to datetime:

data = "01/02/2016 08:01:29;12"
actual_date = datetime.datetime.strptime(data, "%d/%m/%Y %H:%M:%S;%f")

and then you can access both date and time part of that datetime object.

Upvotes: 1

Related Questions