使用Python分析CSV时键入错误

时间:2016-01-18 12:08:49

标签: python csv datetime

这是我第一次发布到本网站,所以如果我遗漏任何内容或做错任何事,请告诉我!

我正在编写一个小程序来分析工作中的一些使用数据(我在教育环境中工作,允许学生预订教室供私人使用)。 基本上,我想将教师预订和学生预订的CSV放入此脚本中,并计算出它们的使用量。

奇怪的是,我在特定的函数上得到了一个TypeError -

def timeelapsed(time1, time2):
    # Function to calculate time elapsed in booking
    start_time = datetime.datetime.strptime(time1, '%Y-%m-%d %H:%M:%S')
    end_time = datetime.datetime.strptime(time2, '%Y-%m-%d %H:%M:%S')
    difference = end_time - start_time
    return difference

奇怪的是,我通过分析没有任何错误的CSV中途得到了这个错误,当我运行调试器时,函数读取的数据很好(time1 = ' 2015-10-08 14:30:00',time2 =' 2015-10-08 15:30:00),所以我有点难过为什么它&#39这样做。

这里是上下文中的代码(对于一塌糊涂的道歉,我还在学习的时候)。

import datetime
import csv

def timeelapsed(time1, time2):
    # Function to calculate time elapsed in booking
    start_time = datetime.datetime.strptime(time1, '%Y-%m-%d %H:%M:%S')
    end_time = datetime.datetime.strptime(time2, '%Y-%m-%d %H:%M:%S')
    difference = end_time - start_time
    return difference

"""
def start_test(time):
    # Function to test whether session starts on the hour or on half hour (unused)
    time_test = datetime.datetime.strptime(time, "%Y-%m-%d %H:%M:%S")
    return time_test.minute
"""

"""
def close_test(end_time, next_time):
    # Function to test whether a session starts close enough to the next session to be considered a single session (UNUSED)
    gaptime = datetime.timedelta(hours=1)
    end_time2 = datetime.datetime.strptime(end_time, '%Y-%m-%d %H:%M:%S')
    next_time2 = datetime.datetime.strptime(next_time, '%Y-%m-%d %H:%M:%S')
    if next_time2 - end_time2 <= gaptime:
        return "Yes"
    else:
        return "No"
"""

def day_of_year(time):
    # Function to return day of year
    day = datetime.datetime.strptime(time, '%Y.%m.%d')
    day_year = day.timetuple().tm_yday
    return day_year


def lessonexpand(time1, time2):
    # Function to expand time slot if it falls on XX:00
    starttime = datetime.datetime.strptime(time1, '%Y-%m-%d %H:%M:%S')
    endtime = datetime.datetime.strptime(time2, '%Y-%m-%d %H:%M:%S')
    if starttime.minute == 0:
        newstarttime = starttime.replace(hour=(starttime.hour - 1), minute=30, second=0, microsecond=0)
        if endtime.minute == 0:
            newendtime = endtime.replace(minute=30, second=0, microsecond=0)
            starttime1 = str(newstarttime)
            endtime1 = str(newendtime)
            return starttime1, endtime1,
        else:
            starttime2 = str(newstarttime)
            endtime2 = str(endtime)
            return starttime2, endtime2
    else:
        if endtime.minute == 0:
            newendtime = endtime.replace(minute=30, second=0, microsecond=0)
            starttime3 = str(starttime)
            endtime3 = str(newendtime)
            return starttime3, endtime3
        else:
            starttime4 = str(starttime)
            endtime4 = str(endtime)
            return starttime, endtime

        # def day_add(tuple1, tuple2):
        #   while day_of_year(tuple1) == day_of_year(tuple2):
        #      length = timeelapsed(*lesson)+


f = open('celcattest2.csv')
csv_f = csv.reader(f)
t = open('celcattest3.csv', "w")
t.close()

for row in csv_f:
    testrow1 = lessonexpand(row[0], row[1])
    testrow2 = timeelapsed(*testrow1)
    dayrow = day_of_year(row[2])
    finalrow3 = str(testrow2), dayrow, row[3]
    print finalrow3
    """with open("celcattest3.csv", "a") as w:
        csv.writer(w).writerow(newrow3)
        """

如果你认为错误是在那里我可以提供CSV文件,但是查看它所有的行都是正确的格式并且在手动输入到抛出类型错误的函数时工作。

任何建议/帮助都会非常感激!

谢谢, 克里斯

编辑:忘记追溯的道歉:

Traceback (most recent call last):
  File "C:/Python27/Time Calculator.py", line 75, in <module>
    testrow2 = timeelapsed(*testrow1)
  File "C:/Python27/Time Calculator.py", line 6, in timeelapsed
    start_time = datetime.datetime.strptime(time1, '%Y-%m-%d %H:%M:%S')
TypeError: must be string, not datetime.datetime

以前的几个打印输出:

('2:00:00', 71, 'G05')
('2:00:00', 106, 'G05')
('2:00:00', 113, 'G05')
('2:00:00', 120, 'G05')
('2:00:00', 127, 'G05')
('2:00:00', 134, 'G05')
('2:00:00', 141, 'G05')
('2:00:00', 148, 'G05')
('2:00:00', 155, 'G05')

1 个答案:

答案 0 :(得分:2)

        starttime4 = str(starttime)
        endtime4 = str(endtime)
        return starttime, endtime

我认为你想要返回starttime4,endtime4而不是starttime,endtime。因为starttime,endtime是datetime类型,当你将它们传递给函数timeelapsed时会导致TypeError:必须是string,而不是datetime.datetime。