Answer the question
In order to leave comments, you need to log in
How to find a line in a txt file and write text under it?
Here I have a text file text.txt
. It contains the following content
. I want to make it look for the string "Members:", and write any text under it
. Like this:
Members:
Hello everyone!
Answer the question
In order to leave comments, you need to log in
Just replay Members:\n to Members:\n and whatever you want
a = "12314142\n12313321\n1243124124\nMembers:\n12313123\n23213123"
print(a.replace('Members:\n', 'Members:\nEto ya dobavil yopta\n'))
The clumsy way - we read the text into an array of strings, look for the desired string and insert new text after it. We write the entire array to a file.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question