-1

Good morning/evening, thank you for taking the time to read this.

I'm a beginner at programming and as part of a coding bootcamp project, I have to create a program to extract data from two .txt files in python, a file with 'user' data and a file with 'task' data.

'user.txt'
# username, password

admin, adm1n
king, king123
batman, batman123
superman, superman123
obiwan, obiwan123
anakin, anakin123
'tasks.txt'
# username, task title, task description, due date, date created, task completed

admin, Register Users, Use taskManager.py to add the usernames and passwords, 10 Oct 2019, 20 Oct 2019, Yes
admin, Assign initial tasks, Use taskManager.py to assign tasks, 10 Oct 2019, 25 Oct 2019, No
batman, Vengeance, Find a way to kill Superman, 12 Dec 2022, 28 Nov 2022, Yes
superman, Survival, Survive Batmans attempt to kill you, 25 Dec 2022, 12 Dec 2022, No
anakin, Vengeance, Embrace the dark side, 13 Mar 2025, 15 Dec 2022, Yes
testing, another test, test 2, 20 Dec 2021, 14 Dec 2022, No

I need to find the total number of tasks assigned to each user. I have managed to read the 'user.txt' file and create a dictionary, but I can't figure out how to read through the 'tasks.txt' file and update the values for each of the keys.

I have managed to extract user data from the 'user.txt' file to a list, and then create a dictionary.

# Empty list
total_users = []
# Read 'user.txt'
with open ("user.txt", "r", encoding = "utf-8") as f:
    # Total number of users
    for line in f:
        line = line.strip().split(", ")
        total_users.append(line[0])

# total users = ['admin', 'king', 'batman', 'superman', 'obiwan', 'anakin']

# Create empty dictionary with username keys
user_tasks = {}
for username in total_users:
    user_tasks.update({username:0})

# user_tasks = {'admin': 0, 'king': 0, 'batman': 0, 'superman': 0, 'obiwan': 0, 'anakin': 0}

I am struggling with how to read certain information from the 'tasks.txt' file and update the values in the dictionary.

I think the pseudocode would be something like this:

for key in user_tasks:
    with open ("tasks.txt", "r", encoding = "utf-8") as f:
        for line in f:
            line_items = re.split(r', |\n', line)
            if line_items[0] == key:
                update key value + 1

# Iterate for each key(user) in dictionary

Essentially, I'm breaking down each line into a list, and trying to use the list item to compare with the dictionary key.

Ideally I would like to display the results in the following format:

Username:
Total tasks assigned to user:
Percentage of total number of tasks assigned to user:
Percentage of assigned tasks completed:
Percentage of assigned tasks to be completed:
Percentage of tasks assigned to be completed that are overdue:
--------------------------------------------------------------
# Iterate for each key(user) in dictionary

I think once I can figure out the total tasks assigned and how to use the dictionary to count, I think the rest should be quite similar.

Any help with the first part would be greatly appreciated.

Thank you again :)

k-sheikh
  • 1
  • 3
  • It's worth your while to [import csv](https://docs.python.org/3/library/csv.html#csv.reader), or perhaps go whole hog with the [pandas](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html) route if you'd like to exploit `.merge()` – J_H Dec 15 '22 at 01:40

1 Answers1

0

I thing if you first load all the data into a dictionary, it could be more easy make the statistics. This is a way to do that.

data = {}
infoTitles = ['task title', 'task description', 'due date', 'date created', 'task completed'] 

# Load Data

# Create first each user as: {admin:'...','...'}
with open('user.txt','r') as usersFile:
    for line in usersFile.readlines():
        user,passw = [value.strip() for value in line.split(',')]
        data.setdefault(user,{'password':passw,'tasks':[]})

#In the dictionary, save the tasks as: tasks:{}
with open('tasks.txt','r') as tasksFile:
    lines = tasksFile.readlines()
    
    # This variable is for the statistics
    totalGlobalTasks = len(lines) 

    for line in lines:
        lineSplited = [value.strip() for value in line.split(',')]
        try:
            data[lineSplited[0]]['tasks'].append({infoTitles[ind]:value for ind,value in enumerate(lineSplited[1:])})
        except KeyError:
            print(f'Username not found: {lineSplited}')

The dictionary format is:

{'username':
    {
        'passw':'..',
        'tasks':[
            {
                'task title':'value',
                'task description':'value', 
                'due date':'value', 
                'date created':'value', 
                'task completed':'value'
            },'..'
        ]
        
    }
    
}
  • try to figure out how to do statistics with this dictionary. If you need help just tell me – Josephdavid07 Dec 15 '22 at 03:30
  • Thanks @Josephdavid07, I tried your solution but I am having an issue with the line: `data.setdefault(user,{'password':passw,'tasks':[]})`, which seems to return a TypeError: unhashable type: list – k-sheikh Dec 15 '22 at 12:05
  • Forgive me, I confused the `strip` function with `split`. I fixed it – Josephdavid07 Dec 15 '22 at 14:45