-1

Could someone help me with getting my program to print user specific information based on which user is logged into the program from info on a txt file. I have a menu in my program which only displays the current users tasks by typing in "vm". This is supposed to show only the tasks assigned to the user. I was able to show the user all tasks for all users but how would I extract only user specific info from a txt file.

The txt file is as follows:

User assigned to task:

admin

Task Title :

Help

Task Description:

Help Granny

Task Due Date:

2020-01-24

Date Assigned:

2020-01-24

Task Completed:

No

...........................

User assigned to task:

johndoe

Task Title :

Walk

Task Description:

Walk the dog

Task Due Date:

2020-01-26

Date Assigned:

2020-01-24

Task Completed:

No

How would I finish my last line of code if I just wanted to print the task for johndoe

The code I have so far is as follows:

import datetime
users = {}
with open ('user.txt', 'rt')as username:
    for line in username:
        username, password = line.split(",")
        users[username.strip()] = password.strip()  # strip removes leading/trailing whitespaces

uinput = input("Please enter your username:\n")
while uinput not in users:
    print("Username incorrect.")
    uinput = input("Please enter a valid username:\n")

if uinput in users:
            print ("Username correct")

with open('user.txt', 'rt') as password:
    for line in password:
        username, password = line.split(",")
        users[password.strip()] = username.strip()  # strip removes leading/trailing whitespaces

uinput2 = input("Please enter your password:\n")
while uinput2 not in users:
    print("Your username is correct but your password is incorrect.")
    uinput2 = input("Please enter a valid password:\n")

if uinput2 in users:
    password2 = ("Password correct")
    print (password2)

if password2 == ("Password correct"):
        menu = (input("\nPlease select one of the following options:\nr - register user\na - add task\nva - view all tasks\nvm - view my tasks\ne - exit\n"))
        if menu == "r" or menu == "R":
                    new_user = (input("Please enter a new user name:\n"))
                    new_password = (input("Please enter a new password:\n"))
                    with open ('user.txt', 'a')as username:
                            username.write("\n" + new_user + ", " + new_password)
        elif menu == "a" or menu == "A":
            task = input("Please enter the username of the person the task is assigned to.\n")
            while task not in username:
                task = input("Username not registered. Please enter a valid username.\n")

            else:
                task_title = input("Please enter the title of the task.\n")
                task_description = input("Please enter the task description.\n")
                task_due = input("Please input the due date of the task. (yyyy-mm-dd)\n")
                date = datetime.date.today()
                task_completed = False
                if task_completed == False:
                    task_completed = "No"
                else:
                    task_completed = ("Yes")
                with open('tasks.txt', 'a') as task:
                    task.write("\nUser assigned to task:\n" + uinput + "\nTask Title :"  + "\n" + task_title + "\n" + "Task Description:\n" + task_description + "\n" + "Task Due Date:\n" + task_due + "\n" + "Date Assigned:\n" + str(date) + "\n" + "Task Completed:\n" + task_completed + "\n") 

        elif menu == "va" or menu == "VA":
            all_tasks = open('tasks.txt', 'r')
            text = all_tasks.read()
            all_tasks.close()
            print(text)
        elif menu == "vm" or menu == "VM"

Any help would really be appreciated. Thank you

  • Appreciate the whole code and file- but as for your requirement, is it that you are scanning a text file: if you enter the value for "User assigned to task:" it should show the value for "Task description" as output? eg : if you enter "johndoe" it should show 'walk the dog' ? – instinct246 Jan 26 '20 at 08:52
  • Thank you for the reply instinct246. But it should show the whole task and description any ideas? –  Jan 26 '20 at 09:22
  • It should show the values for both 'Task title' and 'Task description' for a given name in the text file? – instinct246 Jan 26 '20 at 09:36
  • I just checked the task description... All it says is display all tasks assigned to the user... So if there was more than one task it needs to display all tasks. Im sure it's fine to just display the task on its own. –  Jan 26 '20 at 09:40
  • I have posted the answer. Though you have not posted a desired output, I hope this is what you are looking for. Please accept the answer if this works for you ...else let me know the desired output. – instinct246 Jan 26 '20 at 11:04

1 Answers1

0

This piece of code uses regex and extracts the desired fields from the kind of text file you have pasted above. I copied the exact content you have posted into a text file. I named the file as sample_text.txt

Option 1: This will extract all the user names, the task and the task description they are assigned with.

import re
with open('sample_text.txt','r') as fl:
    lst = re.findall('.*\n*(User assigned to task:).*\n*(.*)\n*(Task Title :)\n*(.*)\n*(Task Description:)\n*(.*)\n*', fl.read())
for item in lst:
    print(f'"{item[1]}" is assigned with "{item[5]}"')


Output:
> "admin" is assigned to "Help Granny"
  "johndoe" is assigned to "Walk the dog"

Option 2: This will ask you the user name you are interested in and then will extract those user details only.

import re
name = input('Enter the name of the user:')
with open('sample_text.txt','r') as fl:
    lst = re.findall('.*\n*User assigned to task:.*\n*(' + re.escape(name) + ')\n*Task Title :\n*(.*)\n*Task Description:\n*(.*)\n*', fl.read())

for item in lst:
    print(f'"{item[0]}" is assigned with task "{item[1]}" and task description "{item[2]}"')

Output:
> Enter the name of the user:johndoe
> "johndoe" is assigned with task "Walk" and task description "Walk the dog"
instinct246
  • 960
  • 9
  • 15
  • Thank you instinct246 this works! Would it be possible to display all of the info? –  Jan 26 '20 at 13:53