Python: Find time difference between consecutive lines in a text file -
I have a text file that contains the following content:
12: 10:
I would like an output like this:
29 DU: 41.016 U 12: 11: 10.517D12: 11: 11.516D12: 12: 01.233 1 DD 80 UD
, which means (i + 1) - line-i-th line -> time difference in seconds + tag (u / d).
I am coming for the number
, line in the line (open ("foo.txt")):
But getting confused; How do I use the 'num' variable?
Whatever you are trying to do will not work because you do not have an array of lines for the don index When you are running again, you only have the current line, but to calculate the difference of time, you need access to both the existing line and the previous line.
Instead, I would like to repeat the lines of lines again, such as importing the importatulos to import the dimensions from time to time; From IIPIP import partial DAP pair (itrebil): "" (pairs, ([0, 1, 2, 3])) [(0, 1), (1, 2), (2, 3)] "" "A , B = T (again) next (B, none) back izip (A, B) DRF parse_time (time_str, time_format = '% H:% M:% S.% f'): return datetime.strptime (time_str, Time_format) with the open ('foo.txt') as F (line0, flag0), (time 1, flag1) Line (line.split ()) for line in line: delta = parse_time (time1) - parse_time (time0) print '{} {} - {}'. Format (delta.total_seconds (), flag1, flag 0) / Code>
It gives time difference in the float, you can always round / sort it if you want. Note: I do not think how you get 80 seconds of the last line in your example Do it, should not be 50 seconds?
Comments
Post a Comment