-1

I am trying to print all of the logged-in users tasks from a file(tasks.txt). This is what I have so far but it only prints out one task.

elif choice == "vm":

         for task in taskList:
             taskItems = task.split(":")

             if loginUsername.strip() == taskItems[0].strip():
                 print(taskItems)

tasks.txt:

Shaun, Admin, Filing, 2020/4/09, 15/4/2020, No
Ashraf, Operations, Quote new air-condition, 2020/3/09, 10/4/2020, No
Clayton, IT, Finish Project, 2020/4/03, 30/4/2020, No
Shaun, Admin, Emails, 2020/4/07, 12/4/2020, No
Shaun, Admin, Data Captures, 2020/4/09, 13/4/2020, No
Roland, Marketing, Marketing Update, 2020/4/01, 10/4/2020, No
Therlow, Sales, Print New Leads, 2020/4/02, 4/4/2020, No
Shaun, Admin, Reply to Lerintha via email, 16/4/2020, 2020/04/15, No
Toni, Deliveries, Deliver all stock, 17/4/2020, 2020/04/16, No    
james bond
  • 21
  • 4

2 Answers2

0

Since you're working with comma-separated values, you can read tasks.txt as a CSV file. This assumes the usernames are always in the 1st column and tasks in the 3rd column (Filing, Finish Project, etc.)

import csv
with open('tasks.txt') as csv_file:
    csv_reader = csv.reader(csv_file)
    for row in csv_reader:
        if loginUsername.strip() == row[0].strip():
            print(row[2])

This approach removes the explicit call to split(',') as commas are the default delimiter for csv_reader().

SteveK
  • 995
  • 5
  • 17
0

Like SteveK, I assumed that you user name is in the first and the task in the third column:

with open ('tasks.txt', 'r') as f:
    lines = f.readlines()

userTasks = dict()
for line in lines:
    try:
        user = line.split(',')[0].strip()
        task = line.split(',')[2].strip()
        userTasks.setdefault(user, []).append(task)
    except IndexError:
        continue

for user in userTasks:
    print('{}: {}'.format(user, userTasks[user]))

Produces this output:

Shaun: ['Filing', 'Emails', 'Data Captures', 'Reply to Lerintha via email']
Ashraf: ['Quote new air-condition']
Clayton: ['Finish Project']
Roland: ['Marketing Update']
Therlow: ['Print New Leads']
Toni: ['Deliver all stock']

Update 1:

Added try and except to account for invalid lines

Update 2:

OP is looking for the complete lines from tasks.txt:

with open ('tasks.txt', 'r') as f:
    lines = f.readlines()

loginUsername = 'Shaun'
userTasks = [line for line in lines if line.lstrip().startswith(loginUsername)]
print(''.join(userTasks))

Produces this output:

Shaun, Admin, Filing, 2020/4/09, 15/4/2020, No
Shaun, Admin, Emails, 2020/4/07, 12/4/2020, No
Shaun, Admin, Data Captures, 2020/4/09, 13/4/2020, No
Shaun, Admin, Reply to Lerintha via email, 16/4/2020, 2020/04/15, No
Stefan Scheller
  • 953
  • 1
  • 12
  • 22
  • Thanks for the feedback. If I run this it gives me a 'list index out of range message'. Also, If I'm logged in as Shaun I would like the program to only print out all the lines/tasks Shaun is listed in etc. – james bond Apr 16 '20 at 10:34
  • Does your input contain colons instead of commas as in the first input you provided? If you want to use this sign as delimiter too, you could change the code to `line.split(',:')...`. If you only want the tasks of one specific user, just use his login as dictionary key like `userTasks.get('Shaun')` or with a variable `userTasks.get(loginUsername)`. – Stefan Scheller Apr 16 '20 at 11:24
  • My input doesn't contain any colons. I'm not exactly sure how to embed 'userTasks.get(loginUsername)this into my loop. Sorry very new to this. – james bond Apr 16 '20 at 14:02
  • @jamesbond: The index error can also be caused by an empty line or any line that contains less than two commas. I added `try` and `except` to the code I posted to account for that. – Stefan Scheller Apr 16 '20 at 14:42
  • I got it working without the try and except but how do I print only the full line/s of the logged in user? I have tried all sorts of ways but I can't seem to get it to work. – james bond Apr 16 '20 at 14:50
  • @jamesbond: You do not need a loop anymore to output all tasks of a user. Load the tasks as in the code I posted. To output the tasks of a user do something like `print(userTasks.get(loginUsername.strip()))`. – Stefan Scheller Apr 16 '20 at 14:51
  • @jamesbond: Sorry can you clarify your question and give an example of your desired output? My understanding was that you want a list of tasks for each user in tasks.txt – Stefan Scheller Apr 16 '20 at 14:56
  • Shaun, Admin, Filing, 2020-4-09, 15/4/2020, No – james bond Apr 16 '20 at 15:02
  • Shaun, Admin, Emails, 2020-4-07, 12/4/2020, No – james bond Apr 16 '20 at 15:03
  • Shaun, Admin, Data Captures, 2020-4-09, 13/4/2020, No – james bond Apr 16 '20 at 15:03
  • Shaun, Admin, Reply to Lirentha via email, 16/4/2020, 2020-04-15, No – james bond Apr 16 '20 at 15:04
  • @jamesbond: So you basically just need all lines that start with loginUsername? – Stefan Scheller Apr 16 '20 at 15:05
  • If Shaun was the logged in user all his lines should be printed like that. – james bond Apr 16 '20 at 15:07
  • yes and if Ashraf or Clayton was logged in only there lines. – james bond Apr 16 '20 at 15:08
  • @jamesbond: I updated my answer. Please consider accepting the answer if that solved your problem. – Stefan Scheller Apr 16 '20 at 15:14
  • It works but only solves it if Shaun is logged in. If any of the other users is logged in it won't suffice becuase it will print Shaun's tasks. – james bond Apr 16 '20 at 15:54
  • The code line `loginUsername = 'Shaun'` should only serve as an example. Set `loginUsername` in your program to the name of the user whose tasks you want to retrieve. – Stefan Scheller Apr 16 '20 at 16:00
  • This does work but the problem that I have is that loginUsername is a global username and if I login in with any other user it doesn't work automatically. I want it to display the tasks of the user logged in without having to hard code loginUsername. – james bond Apr 16 '20 at 16:58
  • @jamesbond: Then repeat the retrieval with updated `loginUsername` when the logged in user changes!? – Stefan Scheller Apr 16 '20 at 17:04
  • My lecturer doesn't want to accept this solution for some reason. He says he doesn't want a set loginUsername in the code. – james bond Apr 20 '20 at 10:14
  • @jamesbond: You don't have to write the user name as a string literal in the code but I cannot know how you retrieve the name of the currently logged in user in the rest of your program. Remove the line ```loginUsername = 'Shaun'``` if this variable already exists in your code (and contains the name of the current user) or replace it with something like ```loginUsername = methodThatReturnsTheCurrentlyLoggedInUser()```. – Stefan Scheller Apr 20 '20 at 12:13