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

标签 python time strftime

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

import time
import strftime from time

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

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

最佳答案

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

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

应该做的伎俩!

关于python - 在 Python 中使用 %f 和 strftime() 来获得微秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6677332/

相关文章:

python - asyncio.iscoroutinefunction 为异步生成器返回 False

python - 删除 Pandas Dataframe 中的重复日期并合并值 - Python

ubuntu - 如何使用 AWK 将时间戳转换为 UTC 时间

python - 我无法在 heroku 上部署我的 flask 应用程序

Python os.listdir() 不断跳过某些文件?

Java:getMinutes 和 getHours

javascript - 在 jQuery 中一次只能有一个元素是 "toggleClass"-ed

javascript - jQuery 时间选择器

PHP strftime 返回 false

c++ - strftime 将不需要的字符添加到我要显示的内容中