字符串到十进制秒的时间

Ric*_*ard 4 python time python-2.7

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

这就是我认为你会这样做的方式:

import time
time.strptime(timestring, '%H%M%S')
Run Code Online (Sandbox Code Playgroud)

但是%S,根据时间文档,不考虑秒的分数.

gan*_*esh 19

你将不得不使用%f

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

使用datetime是正确的

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

  • Nope-`python -c"导入时间;打印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)`-there [are Python的`struct_time`中没有毫秒(http://docs.python.org/2/library/time.html#time.struct_time) (2认同)