0

I have written some code to take in users and assign tasks to them using a text file. The problem I am having is as the user inputs a task number to edit, but I don't know how to replace the line of text 12 lines above the task number that is input and this replaces the username. If there are two tasks in the text file I need to be able to use the task number line as a reference point and then replace the 12th line above that to a new username. So I basically need to replace the 12th line above the users input for every task. I have written some code to just replace the name but its erasing my whole text file.

Example of text file: (tasks.txt)

User assigned to task:

jack

Task Title :

Fly

Task Description:

Fly to the moon

Task Due Date:

2020-02-20 Date Assigned:

2020-02-18

Task Completed:

No

Task number:

1

The code of the effected block so far is:

with open('tasks.txt') as xxaz:
                 main2 = "Task number:" + "\n" + str(review)
                 aa = xxaz.read()
                 if main2 in aa:
                         print(str(main2) + "\n")
                         edit = input("Enter ""1"" to edit the user assigned to task, ""2"" to change the due date or ""3"" to change the completion status.\n")
                         if edit == "1":
                             new_name = input("Please enter a new user name")
                             lines = open('tasks.txt').read().splitlines()
                             lines[2] = new_name
                             open('tasks.txt','w').write
devinbost
  • 4,658
  • 2
  • 44
  • 57

2 Answers2

0

Is this what you are looking for? Personally I will adopt pandas for this. But this is aligned to your approach.

Sample input file:

User assigned to task:
jack
Task Title :
Fly
Task Description:
Fly to the moon
Task Due Date:
2020-02-20
Date Assigned:
2020-02-18
Task Completed:
No
Task number:
1
User assigned to task:
jill
Task Title :
Walk
Task Description:
Walk to the moon
Task Due Date:
2020-03-20
Date Assigned:
2020-02-19
Task Completed:
No
Task number:
2
User assigned to task:
Brenda
Task Title :
Run
Task Description:
Run to the moon
Task Due Date:
2020-03-16
Date Assigned:
2020-04-19
Task Completed:
Yes
Task number:
3

Code

#Take the user inputs
review = input('Enter the task number you want to modify: ')
field = input('''Enter:
1: To change the user assigned to task.
2: To change the due date.
3: To change the completion status
''')
value = input('Enter the value: ')

#Create a function which returns the field name as per the number provided by the user
def switchdic(num):
    dic = {'1':'User assigned to task:',
          '2':'Task Due Date:',
          '3':'Task Completed:'
          }
    return dic.get(num,'Invalid entry')

#code block to read and write the modified content to a new file
with open('user_task_data.txt') as fh,open('new_task_data.txt','w') as fw :
    lines = fh.readlines()
    for line in lines:
        #Skip the lines if they do not match the 'Task number:' as the previous line
        if (lines[lines.index(line)-1].strip() != "Task number:"):
            continue
        data = lines[lines.index(line)-13:lines.index(line)+1] #Assign the whole block of the given task number to variable data
        #Modify the values as per the given input and write to the new file
        if review == str(line.strip()):
            data[lines.index(switchdic(field)+"\n")+1] = value+"\n"
            fw.write(''.join(data))
        #Write the values as they are if they are not connected to the input task number
        else:
            fw.write(''.join(data)) 
print('The file is modified and new file is"new_task_data.txt" ')

O/P

Enter the task number you want to modify: 3
Enter:
1: To change the user assigned to task.
2: To change the due date.
3: To change the completion status
3
Enter the value: No
The file is modified and new file is"new_task_data.txt" 
instinct246
  • 960
  • 9
  • 15
  • Thank you for the reply instinct but I'm looking for a way to actually change the users and status of existing tasks... By input. So say for instance I enter "3", I will je able to edit the username and status of completion for only that task –  Feb 18 '20 at 09:46
  • @Bobby1: The above code in my answer modifies the details for only that particular input task not all the tasks. You may test it and let me know in case you see that it is changing the details for all other tasks.I tested works fine for me. – instinct246 Feb 18 '20 at 09:56
  • Thanks @instinct I'm gonna give it a try! –  Feb 18 '20 at 10:01
  • @Bobby1: Please note it writes the modified content to a new file. That means the new file will contain the old data with your desired fields modified. If you do not want the old file anymore, you may further override it with the newly created file. – instinct246 Feb 18 '20 at 10:05
-1

Here is the solution if you want to continue with your same approach:

Tasks.txt:

User assigned to task:
jack
Task Title :
Fly
Task Description:
Fly to the moon
Task Due Date:
2020-02-20 Date Assigned:
2020-02-18
Task Completed:
No
Task number:
1
User assigned to task:
qwe
Task Title :
Fly
Task Description:
Fly to the moon
Task Due Date:
2020-02-20 Date Assigned:
2020-02-18
Task Completed:
No
Task number:
2

Code:-

with open(r"C:\Users\xyz\Desktop\Tasks.txt","r+") as xxaz:
    main2 = "Task number:" + "\n" + str(2)
    if main2 in xxaz.read():
        edit = input("Enter ""1"" to edit the user assigned to task, ""2"" to change the due date or ""3"" to change the completion status.")
        if edit == "1":
            xxaz.seek(0)
            taskLines = xxaz.readlines()
            new_name = input("Please enter a new user name: ")
            for i in taskLines:
                if i == ('Task number:\n'):
                    indexVal = taskLines.index('Task number:\n')
                    taskLines.insert(indexVal,taskLines[indexVal]+taskLines[indexVal+1])
                    taskLines.pop(indexVal+1)
                    taskLines.pop(indexVal+1)
            indexVal = taskLines.index(main2.strip())
            taskLines.pop(indexVal-10)
            taskLines.insert(indexVal-10,new_name+'\n')
            xxaz.seek(0)
        xxaz.writelines(taskLines)
Vaibhav Jadhav
  • 2,020
  • 1
  • 7
  • 20