在 Python 中使用带 strftime()的% f 获取微秒

我试图将 strftime ()用于微秒级精度,这似乎可以使用% f (如表示的 给你)。但是,当我尝试以下代码:

import time
import strftime from time


print strftime("%H:%M:%S.%f")

... 我得到了小时,分钟和秒,但% f 打印为% f,没有微秒的迹象。我正在 Ubuntu 上运行 Python 2.6.5,所以它应该没问题,并且应该支持% f (据我所知,它支持2.6及以上版本)

251128 次浏览

您正在查看错误的文档。 time模块 有不同的文件

你可以这样使用 datetime模块 strftime:

>>> from datetime import datetime
>>>
>>> now = datetime.now()
>>> now.strftime("%H:%M:%S.%f")
'12:19:40.948000'

您可以使用 datetime 的 strftime 函数来获得这个结果。问题是 time 的 strftime 接受一个不携带微秒信息的时间元组。

from datetime import datetime
datetime.now().strftime("%H:%M:%S.%f")

应该能行!

您还可以使用 time()函数从 time模块获得微秒精度。
(time.time()以秒为单位返回自纪元以来的时间。它的小数部分是以微秒为单位的时间,这正是您想要的。)

>>> from time import time
>>> time()
... 1310554308.287459   # the fractional part is what you want.




# comparision with strftime -
>>> from datetime import datetime
>>> from time import time
>>> datetime.now().strftime("%f"), time()
... ('287389', 1310554310.287459)

当表示微秒的“% f”不起作用时,请使用以下方法:

import datetime


def getTimeStamp():
dt = datetime.datetime.now()
return dt.strftime("%Y%j%H%M%S") + str(dt.microsecond)

这个应该有用

import datetime
datetime.datetime.now().strftime("%H:%M:%S.%f")

会印出来的

例如 14:38:19.425961

使用 Python 的 time模块,使用 %f不能获得微秒。

对于那些仍然只想使用 time模块的人,这里有一个解决方案:

now = time.time()
mlsec = repr(now).split('.')[1][:3]
print time.strftime("%Y-%m-%d %H:%M:%S.{} %Z".format(mlsec), time.localtime(now))

您应该得到类似于 2017-01-16东部时间16:42:34.625的结果(是的,我使用毫秒,因为这已经足够了)。

要将代码分解成细节,请将下面的代码粘贴到 Python 控制台中:

import time


# Get current timestamp
now = time.time()


# Debug now
now
print now
type(now)


# Debug strf time
struct_now = time.localtime(now)
print struct_now
type(struct_now)


# Print nicely formatted date
print time.strftime("%Y-%m-%d %H:%M:%S %Z", struct_now)


# Get miliseconds
mlsec = repr(now).split('.')[1][:3]
print mlsec


# Get your required timestamp string
timestamp = time.strftime("%Y-%m-%d %H:%M:%S.{} %Z".format(mlsec), struct_now)
print timestamp

为了便于澄清,我还在这里粘贴了我的 Python 2.7.12结果:

>>> import time
>>> # get current timestamp
... now = time.time()
>>> # debug now
... now
1484578293.519106
>>> print now
1484578293.52
>>> type(now)
<type 'float'>
>>> # debug strf time
... struct_now = time.localtime(now)
>>> print struct_now
time.struct_time(tm_year=2017, tm_mon=1, tm_mday=16, tm_hour=16, tm_min=51, tm_sec=33, tm_wday=0, tm_yday=16, tm_isdst=0)
>>> type(struct_now)
<type 'time.struct_time'>
>>> # print nicely formatted date
... print time.strftime("%Y-%m-%d %H:%M:%S %Z", struct_now)
2017-01-16 16:51:33 EET
>>> # get miliseconds
... mlsec = repr(now).split('.')[1][:3]
>>> print mlsec
519
>>> # get your required timestamp string
... timestamp = time.strftime("%Y-%m-%d %H:%M:%S.{} %Z".format(mlsec), struct_now)
>>> print timestamp
2017-01-16 16:51:33.519 EET
>>>

如果您想要一个整数,请尝试以下代码:

import datetime
print(datetime.datetime.now().strftime("%s%f")[:13])

产出:

1545474382803

如果你想要速度,试试这个:

def _timestamp(prec=0):
t = time.time()
s = time.strftime("%H:%M:%S", time.localtime(t))
if prec > 0:
s += ("%.9f" % (t % 1,))[1:2+prec]
return s

其中 prec是精确的——需要多少个小数位。 请注意,这个函数没有小数部分前导零的问题,就像这里介绍的其他一些解决方案一样。