how to edit entire field values of CSV in Python?
<p>I am trying to manipulate CSV data like this</p>

<pre><code>ID,val1,val2,data1,val3,val4
BIGINT,BOOL,INT,VARCHAR,INT,BIGINT
10000,'F',1,'batman',1,0
20000,'T',0,'robin',1,1
30000,'T',1,'joker',0,1
</code></pre>

<p>to</p>

<pre><code>ID,val1,val2,data1,val3,val4
BIGINT,BOOL,BOOL,VARCHAR,BOOL,BIGINT
10000,'F','T','batman','T',0
20000,'T','F','robin','T',1
30000,'T','T','joker','F',1
</code></pre>

<p>I have written the code, which is printing the row as list after edit but somehow not writing it to file correctly. can somone please help what I am doing wrong here ?</p>

<pre><code>index_list=[]

with open('input.csv') as fr:
                reader = csv.reader(fr)
                reader.next()
                reader.next()
                column_type=(reader.next())
                for index,val in enumerate(column_type):
                        if val=='INT':
                                index_list.append(index)

#this prints the list of fields where change to go
print(index_list)

with open('input.csv', 'rb') as f:
    reader1 = csv.reader(f) 
    reader1.next()
    reader1.next()
    with open('out.csv', 'wb') as fr:
        for row in reader1:
                for key in index_list: 
                        if row[key]==1:
                                row[key]='T'
                        elif row[key]==0:
                                row[key]='F'

                #this row prints correct data
                print(row)


         writer = csv.writer(fr)
         writer.writerows(row)
</code></pre>

<p>You can use <code>pandas</code> and replace all the values as desired. it would look something like:</p>

<pre><code>from StringIO import StringIO
import pandas as pd

TESTDATA = StringIO("""BIGINT,BOOL,INT,VARCHAR,INT
                    10000,'F',1,'batman',1
                    20000,'T',0,'robin',1
                    30000,'T',1,'joker',0""")


df = pd.read_csv(TESTDATA, sep=",", engine='python')
for column in df:
    if "int" in column.lower():
        df[column] = df[column].replace([1], 'T')
        df[column] = df[column].replace([0], 'F')
</code></pre>

<p>which returns:</p>

<pre><code>    BIGINT  BOOL    INT  VARCHAR    INT.1
0   10000   'F'     T    'batman'   T
1   20000   'T'     F    'robin'    T
2   30000   'T'     T    'joker'    F
</code></pre>

<p>after that you can save it back to a CSV file</p>

python csv