Python - replace a line by its column in file
<p>Sorry for posting such an easy question, but i couldn't find an answer on google.
I wish my code to do something like this
code:</p>

<pre><code>lines = open("Bal.txt").write
lines[1] = new_value
lines.close()
</code></pre>

<p>p.s i wish to replace the line in a file with a value</p>

<p>xxx.dat before:</p>

<pre><code>ddddddddddddddddd
EEEEEEEEEEEEEEEEE
fffffffffffffffff

with open('xxx.txt','r') as f:
    x=f.readlines()
x[1] = "QQQQQQQQQQQQQQQQQQQ\n"
with open('xxx.txt','w') as f:
    f.writelines(x)
</code></pre>

<p>xxx.dat after:</p>

<pre><code>ddddddddddddddddd
QQQQQQQQQQQQQQQQQQQ
fffffffffffffffff
</code></pre>

<p>Note:f.read() returns a string, whereas f.readlines() returns a list, enabling you to replace an occurrence within that list.<br>
Inclusion of the <code>\n</code> (Linux) newline character is important to separate line[1] from line[2] when you next read the file, or you would end up with:</p>

<pre><code>ddddddddddddddddd
QQQQQQQQQQQQQQQQQQQfffffffffffffffff
</code></pre>

python file