how to write results in a for loop to multiple CSV files
<p>I have </p>

<pre><code>with open ('~/abc.csv', 'w') as f:
    write1 = csv.write(f)
    write1.writerow(['header1', 'header2', 'header3', 'header4'])

with open ('~/def.csv', 'w') as g:
    write2 = csv.write(g)
    write2.writerow(['header1', 'header2', 'header3', 'header4', 'header5', 'header6'])

for iteration in a_list:
    perform calculations
    result1 = ([h1, h2, h3, h4],[l1, l2, l3, l4],[m1, m2, m3, m4], ...,[])
    for pa in result1:
        write1.writerow(pa)

    def fun(result1):
        result2 = ([n1, n2, n3, n4, n5, n6],[p1, p2, p3, p4, p5, p6], [], ...[])
        for pb in result2:
             write2.writerow(pb)
</code></pre>

<p>Expecting two csv files as</p>

<pre><code>'header1', 'header2', 'header3', 'header4'
h1, h2, h3, h4
l1, l2, l3, l4
m1, m2, m3, m4
      :
</code></pre>

<p>and</p>

<pre><code>'header1', 'header2', 'header3', 'header4', 'header5', 'header6'
n1, n2, n3, n4, n5, n6
p1, p2, p3, p4, p5, p6
</code></pre>

<p>This can be easily done when all the iterations are completed and separate appended(list) result1 and result2 can be easily written to separate files using  writer.writerows(pa). However, I would like to write to csv files in each iteration, so that I wouldn't miss if I have to break the loop for some reason.</p>

<p>Thank you!</p>