在Python中使用%f和strftime()来获得微秒

use*_*924 103 python time strftime

我想使用的strftime()以微秒级精度,这似乎可以使用%F(为说明这里).但是,当我尝试以下代码时:

import time
import strftime from time

print strftime("%H:%M:%S.%f")
Run Code Online (Sandbox Code Playgroud)

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

ada*_*ish 165

您可以使用datetime的strftime函数来实现此目的.问题是时间的strftime接受一个不携带微秒信息的timetuple.

from datetime import datetime
datetime.now().strftime("%H:%M:%S.%f")
Run Code Online (Sandbox Code Playgroud)

应该做的伎俩!

  • 注意`from datetime import datetime`.如果你只是`import datetime`,你将不得不使用`datetime.datetime.now().strftime("%H:%M:%S.%f")` (6认同)
  • 如果你想使用 `%z` 则不行 (2认同)

Joc*_*zel 32

你正在查看错误的文档.该time模块有不同的文档.

您可以像这样使用datetime模块strftime:

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


bal*_*jas 10

使用 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))
Run Code Online (Sandbox Code Playgroud)

你应该得到类似2017-01-16 16:42:34.625 EET 的信息(是的,我使用毫秒,因为它已经足够了)。

要将代码分解为详细信息,请将以下代码粘贴到 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
Run Code Online (Sandbox Code Playgroud)

为了澄清起见,我还在此处粘贴了我的 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
>>>
Run Code Online (Sandbox Code Playgroud)


sal*_*ssi 6

这应该做的工作

import datetime
datetime.datetime.now().strftime("%H:%M:%S.%f")
Run Code Online (Sandbox Code Playgroud)

它会打印出来

HH:MM:SS.microseconds 像这样 14:38:19.425961


Pus*_*ade 5

您还可以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)
Run Code Online (Sandbox Code Playgroud)

  • 你的例子告诉我你很快输入代码.:) (3认同)

小智 5

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

import datetime

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

  • 如果 dt.microsecond 少于 6 位数字,则此方法不起作用。 (3认同)
  • 这是唯一真正对我有用的解决方案。在 Windows 上的 Jython 中,%f 似乎总是打印文字 %f。我想要毫秒,所以使用 str(dt.microsecond)[0:3] (3认同)
  • "%03d"%int(dt.microsecond/1000) -&gt; 这会打印毫秒而不是微秒 (3认同)