简体   繁体   English

比较列表中的项目

[英]Comparing items in a list

I have a text file containing due dates and dates when tasks were assigned and whether they were completed or not.我有一个文本文件,其中包含任务分配的截止日期和日期以及它们是否完成。 I want to write a code that can go through the file and determine whether the tasks are overdue.我想写一段代码,可以通过文件go判断任务是否逾期。 Ive turned each line in the text file to a list and I'm trying to check if the due date is passed the current date and whether the part of the line that comments whether the task is completed is "No".我已经将文本文件中的每一行都变成了一个列表,我正在尝试检查截止日期是否超过了当前日期,以及评论任务是否完成的那一行是否为“否”。

for line in rdfile:
    line.strip("\n")
    thetasks = line.split(", ")

The due date is the 2nd last item in the list and the No for specifying whether the task is completed is the last item in the list.截止日期是列表中的倒数第二项,用于指定任务是否完成的 No 是列表中的最后一项。

Assuming the line is:假设这条线是:

Admin, Assign initial tasks, 14 Apr 2020, 02 Apr 2020, No

After using the code above I have a list containing the items in the line above separated by the comma and space.使用上面的代码后,我有一个列表,其中包含上面行中的项目,由逗号和空格分隔。

Based on your sample and assuming the format of your lines stay the same, you could use this.根据您的示例并假设您的行格式保持不变,您可以使用它。

from datetime import datetime

line = "Admin, Assign initial tasks, 14 Apr 2020, 02 Apr 2020, No"
splitLine = line.split(',')
date_obj = datetime.strptime(splitLine[2].lstrip(), '%d %b %Y')
print(date_obj.strftime('%d-%m-%Y'))
yes_no = splitLine[4].lstrip()
print(yes_no)

This function will do the job:这个 function 将完成这项工作:

from datetime import datetime

def is_overdue(task):
    splited_line = [elem.strip() for elem in task.split(",")]

    # Datetime object with current datetime
    now = datetime.now()

    # Datetime object with due datetime
    due_datetime = datetime.strptime(splited_line[-2], '%d %b %Y')

    # Boolean value representing task completion 
    is_finished = (splited_line[-1] != "No")

    return (not is_finished) and (due_datetime < now)


line = "Admin, Assign initial tasks, 14 Apr 2020, 02 Apr 2020, No"

print(is_overdue(line))
# True

The function will check the given line for 2 things: function 将检查给定行的两件事:

  • If the task's due date is in the past: (due_datetime < now)如果任务的截止日期在过去: (due_datetime < now)
  • If the task is finished (I suppose, that everything else than "No" means, that the task is done here)如果任务完成(我想,除了“否”之外的所有内容都意味着任务在这里完成)

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

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