简体   繁体   English

如何将 csv 文件中的日期与 python 中的变量进行比较

[英]how to compare a date from a csv file with a variable in python

I have a function where I can read out my csv file.我有一个 function,我可以在其中读出我的 csv 文件。 In my csv I have the following 2 information.在我的 csv 中,我有以下 2 条信息。 the studentnumber and when the student passed the exam.学号和学生通过考试的时间。 I need to know which students have passed the exam in less then 4 weeks.我需要知道哪些学生在不到 4 周的时间内通过了考试。 How could I compare these 2 with eachother?我怎么能将这两个相互比较? the dates that I have in my csv file looks something like this: 21-10-2020 15:20我的 csv 文件中的日期如下所示:21-10-2020 15:20

row[0] = studentsname行 [0] = 学生姓名

row[12] = passed date行 [12] = 通过日期

I have tried doing something like this where I compare row[12] with the end_date but then I get an error message saying this我试过做这样的事情,我将 row[12] 与 end_date 进行比较,但后来我收到一条错误消息,说这个

Error code:错误代码:

'<' not supported between instances of 'str' and 'datetime.datetime' “<”在“str”和“datetime.datetime”的实例之间不受支持

My code:我的代码:

from datetime import datetime
end_date = datetime(day=5, month=10, year=2020, hour=23, minute=59)
fastStudent = []
allPassedStudents = []
with open("Mycsv_file, 'r') as read_obj:
    csv_dict_reader = csv.reader(read_obj, delimiter=';')
    for row in csv_dict_reader:
        if row[12]:
            allPassedStudents.append(row[0])
            if row[12] < end_date:
                fastStudent.append[row[0]]
                print(fastStudent)

You are comparing variables that are of different types.您正在比较不同类型的变量。 To compare a string to a date you must convert one to a date or the other to a string.要将字符串与日期进行比较,您必须将一个转换为日期或将另一个转换为字符串。 In this case you want to convert it to a date在这种情况下,您想将其转换为日期

from datetime import datetime
date_string = '2021-12-31'
datetime = datetime.strptime(date_string, '%Y-%m-%d')
print(datetime)

datetime documentation for other string formats其他字符串格式的日期时间文档

That's because row[12] is a str , not datetime .那是因为 row[12] 是str ,而不是datetime

  1. need to change a type of row[12] to datetime, using datetime.strptime .需要使用datetime.strptime将行 [12] 的类型更改为日期时间。 In this case, you shoud use a format '%d-%m-%Y %H:%M' for '21-10-2020 15:20', for example.在这种情况下,例如,您应该为“21-10-2020 15:20”使用格式“%d-%m-%Y %H:%M”。

  2. fastStudent.append[row[0]] -> need to be changed to fastStudent.append(row[0]) . fastStudent.append[row[0]] -> 需要更改为fastStudent.append(row[0])

import csv
from datetime import datetime
end_date = datetime(day=5, month=10, year=2020, hour=23, minute=59)
fastStudent = []
allPassedStudents = []
with open("Mycsv_file", 'r') as read_obj:
    csv_dict_reader = csv.reader(read_obj, delimiter=';')
    for row in csv_dict_reader:
        print(row)
        passedDate = datetime.strptime(row[12], '%d-%m-%Y %H:%M') # to convert a type of row[12] to datetime type, to enable it comparable!
        if passedDate:
            allPassedStudents.append(row[0])
            if passedDate < end_date:
                fastStudent.append(row[0]) # -> I changed [] to ()
                print(fastStudent)
                # ['Tommy'] (for exmample)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM