I have a task where I need to run certain functions based on a text file to count certain text. The first set of instructions I was able to do myself as it is not user based and it is just a general count of text items in the text file (tasks.txt). My problem I am having now is how do I calculate certain things from a text file based on user only. There is currently only one task but if I had more than one task assigned and to a different user how would I do the calculations for that specific user only? Any advice would really be appreciated.
The Text File is: (tasks.txt)
User assigned to task:
bobby
Task Title :
Fly
Task Description:
Fly to the moon
Task Due Date: 2020-04-01
Date Assigned: 2020-03-03
Task Completed:
No
Task number:
1
1st set of tasks: (completed)
The total number of tasks that have been generated
The total number of completed tasks.
The total number of uncompleted tasks.
The total number of tasks that haven’t been completed and that are overdue.
The percentage of tasks that are incomplete.
The percentage of tasks that are overdue.
2nd set of tasks (User Based):
The total number of tasks assigned to that user.
What percentage of the total number of tasks have been assigned to that user?
What percentage of the tasks assigned to that user have been completed?
What percentage of the tasks assigned to that user must still be completed?
What percentage of the tasks assigned to that user have not yet been completed and are overdue?
My code so far:
data2 = open('tasks.txt').read()
count3 = data2.count('Task Title')
count4 = data2.count('Yes')
count5 = data2.count('No')
with open("tasks.txt", "r")as f5:
today = datetime.datetime.today()
overdue = 0
for line in f5:
if not line.startswith('Task Due Date'): continue
field, value = line.split(':')
if field == 'Task Due Date':
if datetime.datetime.strptime(value.strip(), '%Y-%m-%d') < today:
overdue = overdue + 1
ab = (overdue/count3)*100
abb = (count5/count3)*100
print("Total number of tasks: " + str(count3) + "\nTotal number of completed tasks: " + str(count4) + "\nTotal number of incomplete tasks: " + str(count5) + "\n" +
"The percentage of overdue tasks is: " + str(ab) + "%" + "\n" + "The percentage of incomplete jobs is: " + str(abb) + "%")
num_lines = sum(1 for line in open('user.txt'))
print("The number of registered users is: " + str(num_lines) + "\n")
data3 = open('tasks.txt','r').read()
usr_check = input("Please input a user name to write details of that user.\n")
count6 = data3.count(usr_check)
count7 = (count6/count3)*100
print("The user " + str(usr_check) + " has total tasks of: " + str(count6) + "\n" + str(usr_check)+ "'s" + " " + "percentage of total tasks is: "
+ str(count7) + "%")