简体   繁体   English

转换时间格式,字符串到浮点数

[英]Converting time format , string to float

How can I convert a time string '15:04:23' (hours:minuts:seconds) into a float 150423, using python?如何使用 python 将时间字符串 '15:04:23' (hours:minuts:seconds) 转换为浮点数 150423? The following steps had been done yet:以下步骤已经完成:

Initially I have the time/date information in the format seconds since 01.01.2000.最初,我拥有自 2000 年 1 月 1 日以来格式为秒的时间/日期信息。 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 ...这给了我字符串

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.我不知道这有多大意义,但你去了。

do this做这个

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

this will remove the colons and typecast the string into float.Works in both python2 and python3这将删除冒号并将字符串类型转换为 float.Works 在 python2 和 python3

here are two useful links 这里两个有用的链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM