【问题标题】:string to time with decimal seconds带小数秒的时间字符串
【发布时间】:2013-01-09 18:37:57
【问题描述】:

假设我有一个格式为HHMMSS.SS 的字符串,如何将其转换为时间对象?

我认为你会这样做:

import time
time.strptime(timestring, '%H%M%S')

但是,根据time 文档,%S 不考虑秒数。

【问题讨论】:

  • 检查%f 文档中的%f 指令

标签: python time python-2.7


【解决方案1】:

你必须使用 %f

time.strptime('26/01/12 23:50:32.123', '%d/%m/%y %H:%M:%S.%f')

使用日期时间是正确的

>>> from datetime import datetime
>>> a = datetime.strptime('26/01/12 23:50:32.123', '%d/%m/%y %H:%M:%S.%f')
>>> a.microsecond

【讨论】:

  • 不—python -c "import time; print time.strptime('26/01/12 16:31:32.123', '%d/%m/%y %H:%M:%S.%f')" 打印time.struct_time(tm_year=2012, tm_mon=1, tm_mday=26, tm_hour=16, tm_min=31, tm_sec=32, tm_wday=3, tm_yday=26, tm_isdst=-1)—那里are no milliseconds in Python’s struct_time
  • 看来你们都是对的:datettime 确实有 .microsecond 但 time 没有。谢谢 ganesh - 正是我需要的 +1
猜你喜欢
  • 2013-10-10
  • 1970-01-01
  • 1970-01-01
  • 2019-05-28
  • 1970-01-01
  • 1970-01-01
  • 2012-10-01
  • 2018-04-04
  • 1970-01-01
相关资源
最近更新 更多