我有一个文本文件,其中有两列时间值,如下所示:
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:
strip
不起作用,必须指定它才能实际删除新行字符:相关问题 更多 >
编程相关推荐