I have the below section of code which if the user enters "gr" will run.
What I am trying to do is run over the tasks.txt file, append, strip, and split it to reports[] so I can then access each part of the list as needed to update the 2 dictionaries with the relevant info. I will then be opening and writing this info to 2 new .txt files (task_overview.txt + user_overview.txt)
If this doesn't make any sense I'm sorry but I have been struggling with this for ages and I am also very new.
tasks.txt looks like this:
user;Task;description of task;due date;entered date;completed
sam;asdfgg;gfdsa;2024-12-12;2024-03-09;No
admin;werty;ytrew;2024-12-12;2024-03-09;No
sam;plkju;ujklp;2024-02-12;2024-03-09;No
admin;qwaszx;xzsawq;2023-12-12;2024-03-09;No
sam;finish cap2;make the code more user friendly;2024-03-16;2024-03-11;No
gr code :
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,
}
reports = []
with open("tasks.txt", "r") as task:
# This code updates the dictionary as needed
for line in enumerate(task):
stat_dict_task_overview["Tasks"] += 1
# From this point on nothing gets updated to the dictionary
reports = task.readlines()
for line in task:
reports.append(line.strip().split(";"))
for line in task:
if reports[-1] == "Yes":
stat_dict_task_overview["Completed"] += 1
else:
stat_dict_task_overview["Uncompleted"] += 1
if reports[3] < str(datetime.today()):
stat_dict_task_overview["Overdue"] += 1
print(reports)
print(stat_dict_task_overview)
What I am trying to do is run over the tasks.txt file, append, strip, and split it to reports[] so I can then access each part of the list as needed to update the 2 dictionaries with the relevant info. I will then be opening and writing this info to 2 new .txt files (task_overview.txt + user_overview.txt)
I went a bit overboard with the answer, but I've seen this particular question pass by a couple of times with slight differences. I've quite heavily commented my code, and I've split the things into separate functions.
Main thing is, is that you're using a CSV file with a semicolon separator. Using the standard python CSV module, and specifically the
csv.DictReader()will make your life a lot easier. It basically turns the CSV file into a dict (as the method name suggests) and then it becomes very easy to deal with.resulting output: