user12993670
user12993670

Reputation:

Python changing part of a line

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

Answers (2)

Umutambyi Gad
Umutambyi Gad

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

h0r53
h0r53

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

Related Questions