K
K
KeySh1e2020-08-31 20:30:47
Python
KeySh1e, 2020-08-31 20:30:47

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
5f4d32f7ed221436943336.png
. I want to make it look for the string "Members:", and write any text under it
. Like this:


Members:
Hello everyone!

I know how to write to a txt file, but I don't really know how to do it
Thanks in advance

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
SKEPTIC, 2020-08-31
@SteweX

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'))

R
Ronald McDonald, 2020-08-31
@Zoominger

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 question

Ask a Question

731 491 924 answers to any question