Search code examples
pythontexttxt

Grouping specific tasks for various users from a txt. file in python


I have a task where I have to create a summary of each users tasks which are listed in a txt file. For example the summary must include how many tasks are assigned to each user, how many have been completed, how many are overdue etc. I figured out a very simplistic system where I create a list for each user, however this is not modular at all, because if a new user is created it will completely overlook them. I am really struggling to come up with some modular logic, so any help will be greatly appreciated.

The text file is structured as follows: user name, task title, task description, due date, issued date, completed (Yes/No) e.g.:

james, hello, hello world, 30/Jun/2022, 26 May 2022, Yes

I don't have a lot of code for this, as I am not sure where to begin but this is what I have so far:

# For user overview:

# Creating new txt.file for summary to be output in

user_ov = open('user_overview.txt','w+')

# Opening and reading task text file

task_file = open('tasks.txt','r+')
task_content = task_file.readlines()

# Isolating specific users tasks by name

for line in task_content:
    split_task = line.split(", ")
    user_name = split_task_cont[0]
    
    for user_name in task_content:
        user_name = []
        
# Calculating the length of all the created lists  

num_users = len(file_content)

# Writing the values into the txt file

user_ov.write(f"The total number of users is {length_user}\n")
user_ov.write(f"The total number of tasks is {length_task}\n")

Solution

  • You can use dictionary to store the data and you can easily add new user to it.

    Sample code

    file_content = {}
    with open('user_task_data.txt', 'r') as f:
        for line in f.readlines():
            split_list = line.split(',')
            assert len(split_list) == 6
            
            name = split_list[0].strip()
            task_name = split_list[1].strip()
            task_details = {}
            task_details['task_detail'] = split_list[2].strip()
            task_details['due_date'] = split_list[3].strip()
            task_details['issued_date'] = split_list[4].strip()
            task_details['completed'] = split_list[5].strip()
            tasks = {}
            tasks[task_name] = task_details
            if name in file_content:
                if task_name in file_content[name]:
                    file_content[name][task_name].update(task_details)
                else:
                    file_content[name][task_name] = task_details
            else: 
                file_content[name] = tasks
                
    print('total users: ', len(file_content)) 
    print('total tasks: ', sum(len(file_content[user]) for user in file_content))
    

    say the input is

    James, task1, task 1 details, 30/Jun/2022, 26 May 2022, Yes
    Alan, task2, task 2 details, 03/Jul/2022, 05 Apr 2022, Yes
    Bob, task3, task 3 details, 26/Sep/2022, 21 Feb 2022, No
    James, task4, task 4 details, 15/Jun/2022, 17 Mar 2022, No
    

    The output will be

    {'Alan': {'task2': {'completed': 'Yes', 'due_date': '03/Jul/2022', 'issued_date': '05 Apr 2022', 'task_detail': 'task 2 details'}}, 'Bob': {'task3': {'completed': 'No', 'due_date': '26/Sep/2022', 'issued_date': '21 Feb 2022', 'task_detail': 'task 3 details'}}, 'James': {'task1': {'completed': 'Yes', 'due_date': '30/Jun/2022', 'issued_date': '26 May 2022', 'task_detail': 'task 1 details'}, 'task4': {'completed': 'No', 'due_date': '15/Jun/2022', 'issued_date': '17 Mar 2022', 'task_detail': 'task 4 details'}}}

    The reason I used dict instead of list is to have better time complexity. You can use database to have better results