0

trying to complete a task for my course;

I have two files that I need to read from and compare to get a count. One file contains login information in the format of: username, password

The other file has the tasks in the format of: username, task name, task descrip, date logged, due date, is completed?(Y/N)

I need to read both files and count how many tasks a user in the login information file has in the task file.

This is what I have so far:

user_file = open("user.txt", "r")
user_tasks = open("tasks.txt", "r")

num_users = user_file.readlines()
total_num_users = len(num_users) # This is for a separate part of the task

task_lines = user_tasks.readlines()
my_dict = {}

# TODO use all_file_count for total number of tasks

for user, task in zip(num_users, task_lines):
    user = user.strip("\n").split(", ")
    task = task.strip("\n").split(", ")
    if user[0] == task[0]:
        my_dict = dict(zip(user, task))
print(my_dict)

Initially I was just trying to test if I could make it work and display the user and task but I am not sure how to transform it into a count, although I can see that my first hurdle is the above isn't working correctly as I don't have all my users and their tasks that appear in both files.

This is the output:

/Users/Joekelly/PycharmProjects/HyperionDev/venv/bin/python /Users/Joekelly/PycharmProjects/HyperionDev/main.py {'admin': 'admin', 'adm1n': 'Register Users with taskManager.py'}

For reference:

I have attached screenshots of the txt. files enter image description here Please note: This is not real data I am not sharing people's login/passwords :)

pmqs
  • 3,066
  • 2
  • 13
  • 22

1 Answers1

1

Step 1. Improve your variable names

Most importantly, in your code "num_users" is NOT the number of users, which readers will find confusing.

Step 2. Don't try to zip the lists together

That would only work if the two files were exactly parallel, i.e. the #4 line in one file directly corresponds to the #4 line of the other. I think this is not the case for you, because they are separate lists that only share the value of "user".

Step 3. Build up your dictionary in three stages

First, an empty dictionary, as you have already done.

Then, add an empty list for each user in the user file.

Then, loop through each line in the tasks file, find out which user it belongs to, and add it to the list of tasks of that user.

users_file = open("user.txt", "r")
user_lines = user_file.readlines()

tasks_file = open("tasks.txt", "r")
task_lines = user_tasks.readlines()

tasks_by_user = {}
for user_line in user_lines:
    user = user_line.strip("\n").split(", ")[0] 
    tasks_by_user[user] = 0

for task_line in task_lines:
    user = task_line.strip("\n").split(", ")[0]  
    if user in tasks_by_user:
        tasks_by_user[user] += 1
    else: 
        print("Omitting task for a user not in the users list:", task_line)

print(tasks_by_user)
ProfDFrancis
  • 8,816
  • 1
  • 17
  • 26
  • Thank you @Eureka, will give it a go - yeah I am really struggling with naming variables in this task because it's so long and if I reuse a variable name in a different section I get a error saying shadows name '' from outer scope, something I need to further understand on how to manage :) – Livvy Wilson Feb 26 '23 at 12:26
  • @Eureka I'm sure you have a few mistakes. You should run your code before posting. – quamrana Feb 26 '23 at 12:32
  • @Eureka I am getting this error when I run the code: Traceback (most recent call last): File "/Users/Joekelly/PycharmProjects/HyperionDev/main.py", line 35, in tasks_by_user[user].push(task) ^^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'list' object has no attribute 'push' I haven't used or heard of .push before so not sure how to resolve? – Livvy Wilson Feb 26 '23 at 12:33
  • Sorry should have been "append". I spend too much time in Javascript! – ProfDFrancis Feb 26 '23 at 12:34
  • Ah ok that would make more sense, although now I am getting this error: Traceback (most recent call last): File "/Users/Joekelly/PycharmProjects/HyperionDev/main.py", line 35, in tasks_by_user[user].append(task) ~~~~~~~~~~~~~^^^^^^ KeyError: 'livvy.w23' What I want optimally is to get just the number of tasks a user has rather than just the username as the key and then their tasks as the values. So ideally I would have key = username, value = number of times they have a task in my task file. Sorry if I am not being clear enough @Eureka – Livvy Wilson Feb 26 '23 at 12:41
  • OK, so we need to do two things: (a) instead of a list, to which we append, we start with a zero, to which we add 1 each time. (b) we need to handle cases of tasks addressed to a user that doesn't exist. I have updated. – ProfDFrancis Feb 26 '23 at 12:46
  • 1
    @Eureka apologies, I did try but because I am new it says I need a reputation of 15 to upvote. – Livvy Wilson Feb 26 '23 at 13:03
  • That's a shame. Is there a button to mark it as correct? It should be a big tickmark (checkmark) – ProfDFrancis Feb 26 '23 at 13:04