访问.txt文件中的日期并与今天日期比对
请原谅我,如果这看起来是个简单的解决办法,我刚开始学习编程,非常感谢你们的帮助。
我想要获取到截止日期(下面列表中的第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)
1 个回答
1
我觉得你的代码有几个问题。
首先,语法 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)