如果这看起来是一个简单的解决方案,请原谅我,因为我还处于编码之路的早期,并提前感谢您的帮助。
我正在尝试访问截止日期(下面列表中的元素 3)并与今天的日期进行交叉检查以查看任务是否过期。
这是列表所在的文件 (tasks.txt): 这被解读为 - 用户任务分配给;需要做什么;任务描述;到期日;添加任务时;任务完成了。
sam;needs doing;now;2004-04-12;2024-03-05;No
sam;please do;gym;2024-05-20;2024-03-05;No
admin;do;sd;2004-12-12;2024-03-05;No
dam;dam;dam;2024-12-12;2024-03-05;No
到目前为止我整理的代码是:
elif menu == 'gr':
# creating dictionary with values for task_overview file
stat_dict_task_overview = {
"Tasks": 0,
"Completed": 0,
"Uncompleted": 0,
"Overdue": 0,
"Percentage Incomplete": 0,
"Percentage Overdue": 0
}
# creating dictionary with values for user_overview file
stat_dict_user_overview = {
"Users": 0,
"Tasks": 0,
"Name": 0,
}
# open tasks.txt as read
tasks = open("tasks.txt", "r")
# Split contents task.txt by ';'
for count, line in enumerate(tasks):
split_by_semi = line.split(';')
# unpack elements in task.txt into corresponding variables
user = split_by_semi[0]
how_many_tasks = split_by_semi[1]
description_task = split_by_semi[2]
due_date = split_by_semi[3]
date_added = split_by_semi[4]
completed = split_by_semi[5]
# Add 1 to "Tasks" for each line
stat_dict_task_overview["Tasks"] += count + 1
# Add 1 to "Completed if "Yes"
if[5] == "Yes":
stat_dict_task_overview["Completed"] += count + 1
# Else add 1 to "Uncompleted"
else:
stat_dict_task_overview["Uncompleted"] += count + 1
# Code stuck on needs to check todays date against due date and add 1 if overdue
if[3] < datetime.today().date():
stat_dict_task_overview["Overdue"] += count + 1
# Print results for my ongoing review
print(stat_dict_task_overview)
我认为你的代码有几个问题。
首先,语法
if [5] == 'Yes':
将始终为 False
,因为您正在将字符串 ('Yes'
) 与列表 ([5]
) 进行比较。
其次,如果您使用在循环中定义的变量,则将仅使用文件的最后一行。
第三,更棘手的是,您要使用的行可能需要被删除,以删除尾随的换行符。
第四,你的代码逻辑不是很清楚:如果你想使用字典来计算内容,增量应该是
+= 1
而不是+= count + 1
。
最后,对于日期比较的问题,应该使用这段代码:
due_date_datetime = datetime.datetime.strptime(due_date, "%Y-%m-%d")
if due_date_datetime < datetime.datetime.today():
stat_dict_task_overview["Overdue"] += count + 1
但是,您的整个代码可能应该是:
import datetime
# creating dictionary with values for task_overview file
stat_dict_task_overview = {
"Tasks": 0,
"Completed": 0,
"Uncompleted": 0,
"Overdue": 0,
"Percentage Incomplete": 0,
"Percentage Overdue": 0,
}
# creating dictionary with values for user_overview file
stat_dict_user_overview = {
"Users": 0,
"Tasks": 0,
"Name": 0,
}
# open tasks.txt as read
tasks = open("tasks.txt", "r")
# Split contents task.txt by ';'
for count, line in enumerate(tasks):
# first strip the line to remove trailing `\n`, then split:
split_by_semi = line.strip().split(";")
# unpack elements in task.txt into corresponding variables
user = split_by_semi[0]
how_many_tasks = split_by_semi[1]
description_task = split_by_semi[2]
due_date = split_by_semi[3]
date_added = split_by_semi[4]
completed = split_by_semi[5]
# following lines should be inside the loop, so that you can update
# the dict line by line
# Add 1 to "Tasks" for each line
stat_dict_task_overview["Tasks"] += 1
# Add 1 to "Completed if "Yes"
if completed == "Yes":
stat_dict_task_overview["Completed"] += 1
# Else add 1 to "Uncompleted"
else:
stat_dict_task_overview["Uncompleted"] += 1
# Code stuck on needs to check todays date against due date and add 1 if overdue
# converting to datetime object
due_date_datetime = datetime.datetime.strptime(due_date, "%Y-%m-%d")
# comparing between datetime objects
if due_date_datetime < datetime.datetime.today():
stat_dict_task_overview["Overdue"] += 1
# Print results for my ongoing review
print(stat_dict_task_overview)