unable to write to CSV after replace
<p>I have an input file, in which I am making an string replace operation.</p>

<p>I read the file cell by cell, replace the string and then write it back to a new CSV file.</p>

<pre><code>input_file = open('/Users/tcssig/Desktop/unstacked2.csv', 'r', encoding='utf-8')
output_file = open('/Users/tcssig/Desktop/unstacked3.csv', 'w', encoding='utf-8')
writer = csv.writer(output_file , delimiter=' ')
reader = csv.reader(input_file)
for row in reader:
    for string in row:
       data = [string.replace('read','write')]
       print(data)
       writer.writerow(data)
</code></pre>

<p>Above code runs well, but I get an empty output file.</p>

<pre><code>Example of data :

    reading reading reading reading
interval        0   1   2   3

who axis


Mikael  X   0   10  20  30

Mikael  Y   50  40  30  20

Mikael  Z   100 90  80  70

Mike    X   0   0.1 0.2 0.3

Mike    Y   0.5 0.4 0.3 0.2
</code></pre>

<p>Mike    Z   1   0.9 0.8 0.7</p>

<p>What am i missing?</p>

<p>Close your files: </p>

<pre><code>output_file.close()
input_file.close()
</code></pre>

<p>Also see this answer regarding use of context managers
<a href="http://stackoverflow.com/a/441446/4663466">http://stackoverflow.com/a/441446/4663466</a></p>

python csv