Regex 使用Python根据文件格式提取时间时出错

Regex 使用Python根据文件格式提取时间时出错,regex,python-2.7,Regex,Python 2.7,13:20:06.037 13:20:06.038 13:20:06.039 我想使用python从文件中读取时间戳,并比较相邻值之间的差异。下面是我使用的代码 h, m, s = str(diff).split(':') v,w = str(s).split('.') 我尝试使用split(“:”)将差异拆分为小时、分

13:20:06.037

13:20:06.038

13:20:06.039

我想使用python从文件中读取时间戳,并比较相邻值之间的差异。下面是我使用的代码

h, m, s = str(diff).split(':')

v,w = str(s).split('.')                                                                                       

我尝试使用split(“:”)将差异拆分为小时、分钟和秒。在s中,有秒和毫秒值。当我尝试运行第二行代码时,我得到了错误:“ValueError:需要超过1个值才能解包”

如果要转换文件中的字符串记录,则应尝试:

--put here Your code, that retrieves time records from file--
format = '%H:%M:%S.%f'
time_string = '09:54:11.001'
time = datetime.strptime(time_string, format)
此函数用于显示时间,如您所愿:

strftime("%H:%M:%S.%f", *put your time variable here*)
此代码段显示了如何在格式中获得两个日期之间的差异:

time1 = '09:54:11.001'
time2 = '10:32:43.837'
format = '%H:%M:%S.%f'
difference = datetime.strptime(time2, format) - datetime.strptime(time1, format)
您可以阅读Python文档中有关时间函数的更多信息:


问候。

到目前为止你在尝试什么?可能重复的答案我已经编辑了答案并添加了更复杂的解释。解释得好,我理解得很好