Reputation:
I'm trying to write a todo list program which allows you to add and finish todos, which are stored in a markdown file. I'm trying to replace the empty todo box with a finished one, for the line specified, but I'm not sure how to replace only one, the specified one. Any help would be greatly appreciated. :-)
with open("todo.md", 'r') as file:
data = file.readlines()
print(data)
todoNum = input("Which todo do you want to finish (i.e. 1, 2 etc.): ")
todoNum = int(todoNum)
data[todoNum].replace("- [ ]", "- [x]")
Upvotes: 0
Views: 88
Reputation: 4101
Try this
with open("todo.md", 'r') as file:
data = file.readlines()
print(data)
todoNum = input("Which todo do you want to finish (i.e. 1, 2 etc.): ")
todoNum = int(todoNum)
data[todoNum] = data[todoNum].replace("- [ ]", "- [x]")
Upvotes: -1
Reputation: 3219
The replace function returns a string with the specified replacement. It does not change the data inline. You need to assign the return value.
data[todoNum] = data[todoNum].replace("- [ ]", "- [x]")
Upvotes: 1