在python中更改时间戳的格式 [英] changing the format of timestamp in python

查看:67
本文介绍了在python中更改时间戳的格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将时间戳从一种格式更改为另一种格式.我尝试了一些方法,但无法成功.

I am trying to change the timestamp from one format to another. I tried some approaches but i am not able to succeed.

这是我的时间戳 151005-12:07:34.917928

我想将其更改为这样的格式 2015-10-05 12:07:34:917928

I would like to change it to a format like this 2015-10-05 12:07:34:917928

预先感谢

推荐答案

以下是使用正则表达式的一种方法:

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

这里还有一个使用 datetime 模块的

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

这篇关于在python中更改时间戳的格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆