Python - Add a line behind a specific string with a consecutive numeric
<p>I do have a little problem i can't solve in Python, iam not really familiar with this codes commands, that's one of the reasons this is kind of difficult for me.</p>

<p>For example, when i have a text file like this:</p>

<pre><code>Indicate somename X1
Random qwerty
Indicate somename X2
random azerty
Indicate somename X3
random qwertz
Indicate somename X4
random asdfg
Indicate somename X5
</code></pre>

<p>I would like to make a script to get specific value's behind it, like this:</p>

<pre><code>Indicate somename X1 value = 500
Random qwerty
Indicate somename X2 value = 500
random azerty
Indicate somename X3 value = 500
random qwertz
Indicate somename X4 value = 500
random asdfg
Indicate somename X5 value = 500
</code></pre>

<p>I already tried a script like this: </p>

<pre><code>def replace_score(file_name, line_num, text):
 f = open(file_name, 'r')
 contents = f.readlines()
 f.close()

 contents[line_num] = text+"\n"

 f = open(file_name, "w")
 contents = "".join(contents)
 f.write(contents)
 f.close()

replace_score("file_path", 10, "replacing_text")
</code></pre>

<p>But I couldn't get it working the way i wanted it to do.</p>

<p>I hope someone can help me out,</p>

<p>Greeting,</p>

<pre><code>with open('sample') as fp, open('sample_out', 'w') as fo:
    for line in fp:
        if 'Indicate' in line:
            content = line.strip() + " = 500"
        else:
            content = line.strip()
        fo.write(content + "\n")
</code></pre>

python string