Reputation: 23
So I have an external file where each line has a task formatted like this:
User, Title of task, Description of task, Date assigned, Due date, Completed (Yes/No)
I have created a list of lists where within the main list are lists of the line above essentially where each element is separated from the ", ".
So it looks like this:
[['User', 'Title of task', 'Description of task', 'Date assigned', 'Due Date', 'Completed (Yes/No)']]
I am trying to change the last element of the last list to include "\n" at the end.
Here is the code I implemented:
with open('tasks.txt', 'w') as f2:
count = 0
for i in list_of_tasks:
count += 1
if count == len(list_of_tasks):
list_of_tasks[i][-1] = str(f"{list_of_tasks[i][-1]}\n")
f2.write(", ".join(i))
else:
f2.write(", ".join(i))
This is the error I get:
list_of_tasks[i][-1] = str(f"{list_of_tasks[i][-1]}\n")
~~~~~~~~~~~~~^^^
TypeError: list indices must be integers or slices, not list
Ultimately what I'm trying to achieve is to edit parts of each line in this external file. The initial problem I have is the spacing after writing to the file gets messed up hence I'm trying to figure out how to add \n to the last element in the final list in this list of lists.
if it helps here's the full function:
def view_mine(username):
# opens tasks.txt as read only and assigns it as a variable named f1
with open('tasks.txt', 'r') as f1:
x = 1
list_of_tasks= []
other_tasks = []
for line in f1:
line3_split = line.split(', ')
if line3_split[0] == username:
user_format = f"""
Task {x}: {line3_split[1]}
Assigned to: {line3_split[0]}
Date assigned: {line3_split[3]}
Due date: {line3_split[4]}
Task complete? {line3_split[5]}
Task description:
{line3_split[2]}
"""
print(user_format)
x += 1
list_of_tasks.append(line3_split)
else:
other_tasks.append(line3_split)
selection = int(input(f"Which task do you want to select (1-{x-1}) or enter '-1' to return to main menu? ")) -1
if selection == -2:
return
else:
mark_or_edit = int(input(f"To mark as complete enter '1'. To edit the task enter '2'."))
if mark_or_edit == 1:
if list_of_tasks[selection][-1] == "No":
list_of_tasks[selection][-1] = "Yes\n"
elif list_of_tasks[selection][-1] == "No\n":
list_of_tasks[selection][-1] = "Yes\n"
elif mark_or_edit == 2:
user_or_date = int(input("To edit user assigned enter '1'. To edit due date enter '2'. "))
if user_or_date == 1:
user_check = input("Which user do you want to assign this task to? ")
existing_names_list = []
with open('user.txt', 'r') as f:
for line in f:
existing_names = line.split(', ')
existing_names_list.append(existing_names[0])
if user_check in existing_names_list:
list_of_tasks[selection][0] = user_check
else:
print("User does not exist.")
elif user_or_date == 2:
new_date = input("Enter the new due date (XX XXX XXXX): ")
list_of_tasks[selection][4] = new_date
with open('tasks.txt', 'w') as f2:
count = 0
for I in list_of_tasks:
count += 1
if count == len(list_of_tasks):
list_of_tasks[i][-1] = str(f"{list_of_tasks[i][-1]}\n")
f2.write(", ".join(i))
else:
f2.write(", ".join(i))
for i in other_tasks:
f2.write(", ".join(i))
return
Upvotes: -1
Views: 234
Reputation: 45
The problem is in:
for i in list_of_tasks
In this situation i is a string and you are trying to use it as index in your list.
I am not sure what you are trying to accomplish exactly, but I think your code has some logical inaccuracies. I hope the following code gets the job done:
with open('tasks.txt', 'w') as f2:
count = 0
for sub_list in list_of_tasks:
for i in range(len(sub_list)):
if i == len(sub_list) - 1:
temp = sub_list[i] + '\n'
f2.write(temp)
else:
f2.write(sub_list[i] + ', ')
The output of the above code for the list you provided is:
User, Title of task, Description of task, Date assigned, Due Date, Completed (Yes/No)
with '\n' included in the end, even if it is not obvious.
It seems that you have a list of lists, so a 2D array, in which every element is a string. So you have to loop once for the first dimension (so for each list) and then for the second dimension(so for each string in list). Having every string each time, you are able to create the sentence you are trying to.
Upvotes: 0