Python 3 - How to write specific row in same file csv?
<p>I want to update a fieldname called "done" in same file csv</p>

<p>This is structure of file csv:</p>

<p>Input:</p>

<pre><code>Email            Done
 1@gmail.com       
 2@gmail.com       
 3@gmail.com
 4@gmail.com
</code></pre>

<p>Output:</p>

<pre><code>Email            Done
 1@gmail.com       1
 2@gmail.com       1
 3@gmail.com       1
 4@gmail.com       1
</code></pre>

<p>What I want look like:</p>

<pre><code>import csv
with open(r'E:\test.csv','r', encoding='utf-8') as f:
    reader = csv.DictReader(f,delimiter='|')

    for row in reader:
      #do something here#
      #then write "1" to filename "done"
f.close()
</code></pre>

<p>How to do it ? Thank you very much ! :)</p>

<p>With a simple change like this you could use a simple pair of read and write files; read a line, and write it back out immediately with the addition.</p>

<pre><code>In [6]: with open('test.csv','r') as input:
   ...:    with open('test1.csv','w') as output:
   ...:        header=input.readline()
   ...:        output.write(header)
   ...:        for line in input:
   ...:            output.write('%s %8d\n'%(line.strip(),1))

In [7]: cat test1.csv
Email            Done
1@gmail.com        1
2@gmail.com        1
3@gmail.com        1
4@gmail.com        1
</code></pre>

<p>You could also use a csv reader and csv writer; A <code>numpy</code> user might be tempted to use the <code>loadtxt</code> and <code>savetxt</code> pair, though mixing string and number values in an array takes a bit more know-how.</p>

<p>In a new enough Python you could put both opens in one with context:</p>

<pre><code>with open('test.csv','r') as input, open('test1.csv','w') as output:
    ...
</code></pre>

python python-3.x csv