在 Python 中使用 %f 和 strftime() 来获取微秒 [英] Using %f with strftime() in Python to get microseconds

查看:112
本文介绍了在 Python 中使用 %f 和 strftime() 来获取微秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 strftime() 到微秒精度,这似乎可以使用 %f (如所述 此处).但是,当我尝试以下代码时:

I'm trying to use strftime() to microsecond precision, which seems possible using %f (as stated here). However when I try the following code:

import time
import strftime from time

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

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

...I get the hour, the minutes and the seconds, but %f prints as %f, with no sign of the microseconds. I'm running Python 2.6.5 on Ubuntu, so it should be fine and %f should be supported (it's supported for 2.6 and above, as far as I know.)

推荐答案

你可以使用 datetime 的 strftime 函数来得到这个.问题是 time 的 strftime 接受一个不携带微秒信息的时间元组.

You can use datetime's strftime function to get this. The problem is that time's strftime accepts a timetuple that does not carry microsecond information.

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

应该可以解决问题!

这篇关于在 Python 中使用 %f 和 strftime() 来获取微秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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