2024-04-24 23:09:13 发布
网友
下面是我的数据集
Date Time 2015-05-13 23:53:00
我想在python脚本中将日期和时间转换为作为单独列的浮动。
输出应该像日期一样是20150513,时间应该是235300
20150513
235300
如果有日期时间,可以使用strftime()
your_time.strftime('%Y%m%d.%H%M%S')
如果变量是字符串,则可以使用replace()
dt = '2015-05-13 23:53:00' date = dt.split()[0].replace('-','') time = dt.split()[1].replace(':','') fl = float(date+ '.' + time)
date = "2015-05-13".replace("-", "") time = "10:58:56".replace(":", "")
如果您只需要去掉连字符和冒号,str.replace()应该完成以下工作:
>>> s = '2015-05-13 23:53:00' >>> s.replace('-', '').replace(':', '') '20150513 235300'
对于更复杂的重新格式化,请使用time.strptime()解析输入,然后使用time.strftime()重新格式化:
>>> import time >>> t = time.strptime('2015-05-13 23:53:00', '%Y-%m-%d %H:%M:%S') >>> time.strftime('%Y%m%d %H%M%S', t) '20150513 235300'
如果有日期时间,可以使用strftime()
如果变量是字符串,则可以使用replace()
如果您只需要去掉连字符和冒号,str.replace()应该完成以下工作:
对于更复杂的重新格式化,请使用time.strptime()解析输入,然后使用time.strftime()重新格式化:
相关问题 更多 >
编程相关推荐