值错误:未转换的数据仍然存在:

2024-04-29 16:58:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个文本文件,其中有两列时间值,如下所示:

21:28:07.638502 21:28:07.636

是的,一列有额外的时间数字。我试着读所有的行,得到微秒的时间差。。。等等

如果我尝试下面的方法,我会出错的。

import datetime
format="%H:%M:%S.%f"
with open(file) as fh:
    for line in fh.readlines():
        line.strip()
        [a,b]=line.split(' ')
        dta=datetime.datetime.strptime(a,format)
        dtb=datetime.datetime.strptime(b,format)
        diff=dta-dtb
        print(diff.microseconds)

我不确定“未转换的数据”来自何处。错误信息中的“剩余:”后没有任何内容。数据到底是什么?

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/root/.pyenv/versions/3.5.1/lib/python3.5/_strptime.py", line 500, in _strptime_datetime
  tt, fraction = _strptime(data_string, format)
File "/root/.pyenv/versions/3.5.1/lib/python3.5/_strptime.py", line 340, in _strptime
  data_string[found.end():])
ValueError: unconverted data remains: 

Tags: 数据inpyenvformatdatadatetimeline时间