Skip to content
Advertisement

How to print only the start and endtime from a time log file(txt file) in python 3.8.10

This is the code I am using and I am getting partial output

JavaScript

The txt file consists of the following elements: time.txt

and the output i am getting is output

here in the output the program is not printing till the end of the txt file , it is only printing till a certain line and stopping . dont know the reason.

Advertisement

Answer

Say you have a file called time.txt with the following contents that you know is VALID:

JavaScript

You could utilize a helper function to check if the possible parts of a line for a “starttime”, end with am or pm:

JavaScript

Output:

JavaScript

To calculate the total hours you could parse each time interval into a timedelta:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement