Raj*_*esh 1 datetime timestamp python-2.7
我正在尝试将时间戳从一种格式更改为另一种格式。我尝试了一些方法,但无法成功。
这是我的时间戳 151005-12:07:34.917928
我想将其更改为这样的格式 2015-10-05 12:07:34:917928
提前致谢
这是使用正则表达式的一种方法:
import re
e = '(\d{2})(\d{2})(\d{2})-(\d{2}:\d{2}:\d{2})\.(\d+)'
s = '151005-12:07:34.917928'
print('20{}-{}-{} {}:{}'.format(*re.search(e, s).groups()))
2015-10-05 12:07:34:917928
Run Code Online (Sandbox Code Playgroud)
这是另一个使用该datetime模块的方法:
import datetime
e = '%y%m%d-%H:%M:%S.%f'
d = datetime.datetime.strptime(s, e)
print(datetime.datetime.strftime(d, '%Y-%m-%d %H:%M:%S:%f'))
2015-10-05 12:07:34:917928
Run Code Online (Sandbox Code Playgroud)