-3

How can I convert a time string '15:04:23' (hours:minuts:seconds) into a float 150423, using python? The following steps had been done yet:

Initially I have the time/date information in the format seconds since 01.01.2000. I converted this using the following function

 t_temp = datetime(2000,1,1)+timedelta(seconds = t)

Then I extracted the time

t_temp.strftime("%H:%M:%S")

... which gives me string

  • 2
    Why do you want a float `150423` for `"'15:04:23' "`? That does not make any sense. It just implies that you can perform math on that, e.g. calculate the difference between two times, but you can't. If you want the time as a numeric value, use a proper timestamp. – tobias_k Jan 26 '19 at 11:31
  • I need this to compare two different data set ;) –  Jan 26 '19 at 11:38
  • Do you mean that the dates in the _other_ data set are in this format, and you want to convert yours into the same format? Still, might make more sense to look at the code that produces those other float-dates and fix it. – tobias_k Jan 26 '19 at 11:41
  • that's not possible, because the other data set is produced by the European Centre for Medium-Range Weather Forecasts. I don't have access to their codes ;) –  Jan 26 '19 at 11:59

2 Answers2

1

Just omit the colons and cast the result to a float:

float(t_temp.strftime("%H%M%S"))

I don’t know how much sense this really makes, but there you go.

deceze
  • 510,633
  • 85
  • 743
  • 889
0

do this

time = float(t_temp.replace(":",""))

this will remove the colons and typecast the string into float.Works in both python2 and python3

here are two useful links

awadhesh14
  • 89
  • 7
  • 1
    Not my downvote, but this is probably the most cumbersome and inefficient way to remove those `:`... better just use `str.replace`, or just use a different format for `strftime` in the first place. – tobias_k Jan 27 '19 at 12:12
  • 1
    @tobias_k I tried to address what was asked in the question in an easily understandable way. But you are right. I will make the respective change. – awadhesh14 Jan 27 '19 at 12:19