changing the format of timestamp in python

本秂侑毒 提交于 2021-02-11 08:17:25

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!