我正在为班级做一个项目,你必须从时间表的csv文件中获取信息,到目前为止,我有一个脚本可以计算学分总数,并打印最早和最新的班级。但还有一件事我想不通,那就是把所有课程的总小时和分钟数相加并打印出来。我该如何把它添加到我现有的代码中?
当前代码:
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)
这是时间表:https://i.stack.imgur.com/hEQkS.png
- 对于计划中的每个类
- 将开始和结束时间转换为datetime.time对象
- 结尾减去开头
- 在列表中累积时间增量(?,tuple?(
- 把所有的时间增量加在一起
也许把它放在一个函数中,然后给函数传递一个调度,让函数返回总数。
日期时间