This is the code I am using and I am getting partial output
JavaScript
x
14
14
1
def main():
2
with open('time.txt') as f:
3
linenumber=0
4
for lines in f:
5
a=f.readlines()
6
for i in range(0,len(lines)):
7
print(a[i].split()[1],a[i].split()[3])
8
#print (a[i])
9
10
11
12
if __name__=='__main__':
13
main()
14
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
1
70
70
1
Time Log:
2
2/23/12: 9:10pm - 11:40pm getting familiar with Flash
3
2/29/12: 12:50pm - 2:00pm getting familiar with Flash
4
3/1/12: 6:00pm - 11:40pm getting familiar with Flash
5
3/3/12: 3:00pm - 7:00pm step-debug Energy Game code
6
3/4/12: 8:00pm - 11:40pm start carbon game
7
3/5/12: 2:00pm - 3:00pm
8
4:00pm - 4:30pm carbon game
9
3/6/12: 11:30am - 1:30pm carbon game data structures and classes for the first action
10
3/7/12: 11:00am - 5:00pm tested basic concept
11
3/8/12: 1:00am - 1:30am changed vector calling,
12
10:30am - 2:30pm
13
4:00pm - 5:00pm wrote code to draw points indicator and marshal the data code;
14
3/9/12: 12:00am - 2:30am added CarbonActionCategoryView.as and captured some icons
15
11:30am - 4:50pm research how to resize; labor work to capture icons; research how to use event/delegate;
16
3/10/12: 1:50am - 6:00am event/delegate alternative implementation; score fields implementation
17
10:40am - 1:00pm define CarbonConsts.as for those names; implemented points fields
18
3/12/12: 10:45am - 5:00pm research on scrollpane, to no avail
19
3/13/12: 11:00am - 5:00pm research on Slider customization and make some progress without fully satisfication
20
3/14/12: 12:10pm - 5:00pm continue to research on Slider and got some idea
21
3/15/12: 3:30am - 4:30am experiment with CarbonSlider concept
22
11:00am - 5:00pm "Flash Training with Paul Trani" and continue to experiment with CarbonSlider concept
23
3/16/12: 10:00am - 5:00pm integrate the CarbonSlider class into the Game project
24
8:00pm - 9:00pm added the display of the slider thumb value;
25
9:30pm - 11:00pm clean-up the resources and adjust slider.x; start to modify CarbonPointsIndicator.as
26
3/17/12: 6:00am - 7:00am
27
12:00pm - 12:50pm
28
4:00pm - 7:00pm bad CarbonPointsIndicator modification
29
3/18/12: 9:10am - 9:40am draw 2-pixels rectangle and 1-pixel tick for Points Indicator
30
9:40am - 10:30am debug biofueled cars & trucks, mix in wood in coal plants
31
11:30am - 3:30pm scrollpane implementation - alternative
32
11:00pm - 12:00am read Adobe Flash Professional online help
33
3/19/12: 5:00am - 6:00am read Adobe Flash Professional online help
34
3/20/12: 9:00am - 10:00am discuss with Dr. Lant and Blanca.
35
10:00am - 11:00am slider width will be proportional per page or section, total score is changed to total carbon points, options remaining tops 100
36
3/23/12: 1:30pm - 3:30pm install Adobe Illustrator
37
4:00pm - 5:00pm Flash Professional online help
38
3/29/12: 3:11am - 7:00am background shape study, to no avail
39
11:00am - 12:50pm continue background shape study - no longer needed
40
3:00pm - 5:05pm scroll pane automatically adjusted based on the view size; dynamically created static texts
41
10:25pm - 11:00pm adjust positions of text fields
42
3/30/12: 9:50am - 11:30am loading images in AS 3 training; flash work cycle training
43
11:40am - 2:20pm experiment with ideas; learn illustrator
44
3:20pm - 5:05pm re-captured all .png files; .ai files do not have any advantage over .png files
45
3/31/12: 7:00am - 9:00am change the color of points indicator; add PPU; use Bitmap instead of MovieClip;
46
4/2/12: 12:10pm - 1:42pm study mask and Shape; come up with the concept of carbon progressbar
47
1:43pm - 5:00pm implement the carbon progressbar using mask and shape
48
7:00pm - 7:40pm implement the carbon progressbar by drawing bar and outline
49
11:00pm - 12:05am added total options and options
50
4/3/12: 1:30am - 3:00am added Options In Total static text field; Options Remaining will not go negative.
51
4/4/12: 3:50pm - 5:00pm update the icons
52
8:35pm - 11:50pm continue to update the icons; use the colors in .ai file
53
4/9/12: 12:30pm - 6:00pm update based on feedback
54
9:50pm - 2:30am
55
4/10/12: 8:56am - 9:57am
56
- 10.remove "other"
57
- 9. add "Replace Cement"
58
- 8. move current points slightly to the right
59
- 7. center max points on last tic mark
60
- 6. provide a bit more white space for four large boxes at the top
61
- 5. At top of left-hand column, label boxes "Tons of Carbon per unit"
62
- 4. PPU boxes. Align vertically. Expand. "Points per Unit" at the top of this column.
63
- 3. decrease Improve Insulation limit from 20 to 15
64
- 2. optionsRemaining and optionsUsed
65
- 1. total carbon points bar
66
5/19/12: 4:03pm - 6:57pm update based on teachers' feedback and dynamically dragging slide
67
6/4/12: 3:30pm - 5:04pm prev/more options implementation
68
8:15pm - 10:00pm
69
3/13/13: 2:11pm - 5:12pm Make numbers in the yellow triangles bigger and bolder; Make numbers in the options used and remaining bigger and bolder
70
You could utilize a helper function to check if the possible parts of a line for a “starttime”, end with am
or pm
:
JavaScript
1
18
18
1
def is_time(s: str) -> bool:
2
return s.endswith('am') or s.endswith('pm')
3
4
def main() -> None:
5
with open('time.txt', 'r') as log_file:
6
next(log_file) # Skip first line.
7
for line in log_file:
8
line_parts = line.split()
9
if line_parts[0].count('/') == 2: # Line starts with a date.
10
if is_time(line_parts[1]):
11
print(' '.join(line_parts[1:4]))
12
else:
13
if is_time(line_parts[0]):
14
print(' '.join(line_parts[0:3]))
15
16
if __name__ == '__main__':
17
main()
18
Output:
JavaScript
1
59
59
1
9:10pm - 11:40pm
2
12:50pm - 2:00pm
3
6:00pm - 11:40pm
4
3:00pm - 7:00pm
5
8:00pm - 11:40pm
6
2:00pm - 3:00pm
7
4:00pm - 4:30pm
8
11:30am - 1:30pm
9
11:00am - 5:00pm
10
1:00am - 1:30am
11
10:30am - 2:30pm
12
4:00pm - 5:00pm
13
12:00am - 2:30am
14
11:30am - 4:50pm
15
1:50am - 6:00am
16
10:40am - 1:00pm
17
10:45am - 5:00pm
18
11:00am - 5:00pm
19
12:10pm - 5:00pm
20
3:30am - 4:30am
21
11:00am - 5:00pm
22
10:00am - 5:00pm
23
8:00pm - 9:00pm
24
9:30pm - 11:00pm
25
6:00am - 7:00am
26
12:00pm - 12:50pm
27
4:00pm - 7:00pm
28
9:10am - 9:40am
29
9:40am - 10:30am
30
11:30am - 3:30pm
31
11:00pm - 12:00am
32
5:00am - 6:00am
33
9:00am - 10:00am
34
10:00am - 11:00am
35
1:30pm - 3:30pm
36
4:00pm - 5:00pm
37
3:11am - 7:00am
38
11:00am - 12:50pm
39
3:00pm - 5:05pm
40
10:25pm - 11:00pm
41
9:50am - 11:30am
42
11:40am - 2:20pm
43
3:20pm - 5:05pm
44
7:00am - 9:00am
45
12:10pm - 1:42pm
46
1:43pm - 5:00pm
47
7:00pm - 7:40pm
48
11:00pm - 12:05am
49
1:30am - 3:00am
50
3:50pm - 5:00pm
51
8:35pm - 11:50pm
52
12:30pm - 6:00pm
53
9:50pm - 2:30am
54
8:56am - 9:57am
55
4:03pm - 6:57pm
56
3:30pm - 5:04pm
57
8:15pm - 10:00pm
58
2:11pm - 5:12pm
59
To calculate the total hours you could parse each time interval into a timedelta
:
JavaScript
1
33
33
1
from datetime import datetime, timedelta
2
3
def is_time(s: str) -> bool:
4
return s.endswith('am') or s.endswith('pm')
5
6
def parse_12_hour_time(time: str) -> datetime:
7
return datetime.strptime(time, '%I:%M%p')
8
9
def get_timedelta(start_time: str, end_time: str) -> timedelta:
10
return parse_12_hour_time(end_time) - parse_12_hour_time(start_time)
11
12
def timedelta_to_hours(td: timedelta) -> float:
13
return td.total_seconds() / 3600
14
15
def main() -> None:
16
total_hours = 0
17
with open('time.txt', 'r') as log_file:
18
next(log_file) # Skip first row.
19
for line in log_file:
20
line_parts = line.split()
21
if line_parts[0].count('/') == 2: # Line starts with a date.
22
if is_time(line_parts[1]):
23
td = get_timedelta(line_parts[1], line_parts[3])
24
total_hours += timedelta_to_hours(td)
25
else:
26
if is_time(line_parts[0]):
27
td = get_timedelta(line_parts[0], line_parts[2])
28
total_hours += timedelta_to_hours(td)
29
print(f'{total_hours = :.2f}')
30
31
if __name__ == '__main__':
32
main()
33
Output:
JavaScript
1
2
1
total_hours = 74.38
2