'How can you add up the total hours of a schedule from a .csv file?

I am doing a project for class where you have to get information from a csv file of your schedule and so far I have a script that can calculate the total number of credits and print earliest and latest classes. But there is one more thing I have to do that I can't figure out, which is adding the total number of hours and minutes for all of the classes and printing it. How would I add that on to this code that I have?

Current code:

import csv

creditTotal = 0
earlyTime = 24.0
lateTime = 0.0
schedFile = open('c:/Users/l/Documents/schedule.csv')
schedDict = csv.DictReader(schedFile)

for row in schedDict:
    creditStr = row['Credit']
    if (creditStr != ""):
        creditVal = int(creditStr)
        creditTotal += creditVal
    earlyStr = row['Stime'].replace(":",".")
    earlyVal = float(earlyStr)
    if (earlyVal < earlyTime):
        earlyTime = earlyVal
        earlyRow = row
lateStr = row['Etime'].replace(":",".")
lateVal = float(lateStr)
if (lateVal > lateTime):
    lateTime = lateVal
    lateRow = row

print ("Total Credits Scheduled: ", creditTotal)
print ("Early Start:", earlyRow)
print("Late End:" , lateRow)

Here is the schedule: https://i.stack.imgur.com/hEQkS.png



Solution 1:[1]

  • For each class in the schedule
    • convert the start and end times to datetime.time objects
    • subtract the start from the end
    • accumulate the time deltas in a list(?, tuple?)
  • add all the time deltas together.

Maybe put that in a function and pass the function a schedule and have the function return the total.


datetime

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 wwii