Search code examples
pythondictionarypython-zip

How to create a dictionary to count value (username) appearances in two files? - Python


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 :)


Solution

  • 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)