Q:

unable to write to CSV after replace

I have an input file, in which I am making an string replace operation.
I read the file cell by cell, replace the string and then write it back to a new CSV file.
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)

Above code runs well, but I get an empty output file.
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

Mike    Z   1   0.9 0.8 0.7
What am i missing?

A:

Content of input file:
"Roll No" English read Science
"Roll No" English Write Science

Problem with your code:

As mentioned by @Scott, files are not closed.
Your are reading cell by for string in row: and replacing string there. But after replacement you are writing that cell as row in your file. For example, output file with your code looks file :
Roll No
English
read
Science

This is due to above mentioned reason i.e. you are writing each cell.
How to make it working?
Comments inline with code
import csv
input_file = open('mark.csv', 'r', encoding='utf-8')
output_file = open('result.csv', 'w')
writer = csv.writer(output_file , delimiter=' ', encoding='utf-8')
reader = csv.reader(input_file)
for row in reader:
    #Initailize empty list for each row
    data = []
    for string in row:
       #Replace and add to data list
       data.append(string.replace('read','write'))
    #Now write complete
    writer.writerow(data)

input_file.close()
output_file.close()

Output:
"Roll No" English write Science

"Roll No" English Write Science

You can achieve same thing without csv module.
with open("mark.csv") as input_file:
    with open("result.csv",'w') as output_file:
        for line in input_file:
            new_line = (line.replace("read","write")).replace(","," ")
            output_file.write(new_line)

