问题
I am trying to change the timestamp from one format to another. I tried some approaches but i am not able to succeed.
This is my timestamp 151005-12:07:34.917928
I would like to change it to a format like this 2015-10-05 12:07:34:917928
Thanks in Advance
回答1:
Here's one way using regular expressions:
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
Here's another using the datetime
module:
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
来源:https://stackoverflow.com/questions/32969113/changing-the-format-of-timestamp-in-python