Python append column header & append column values from list to csv
<p>I am trying to append column header (hard-coded) and append column values from list to an existing csv. I am not getting the desired result. </p>

<p>Method 1 is appending results on an existing csv file. Method 2 clones a copy of existing csv into temp.csv. Both methods don't get me the desired output I am looking for. In Results 1, it just appends after the last row cell. In results 2, all list values append on each row. Expected results is what I am looking for. </p>

<p>I have included my code below. Appreciate any input or guidance. </p>

<p><strong>Existing CSV Test.csv</strong></p>

<pre><code>Type,Id,TypeId,CalcValues
B,111K,111Kequity(long) 111K,116.211768
C,111N,B(long) 111N,0.106559957
B,111J,c(long) 111J,20.061634
</code></pre>

<p><strong>Code - Method 1 &amp; 2</strong></p>

<pre><code>final_results = ['0.1065599566767107', '0.0038113334533441123', '20.061623176440904']

# Method1
csvfile = "test.csv"
with open(csvfile, "a") as output:
    writer = csv.writer(output, lineterminator='\n')
    for val in final_results:
        writer.writerow([val])  

# Method2
with open("test.csv", 'rb') as input, open('temp.csv', 'wb') as output:
    reader = csv.reader(input, delimiter = ',')
    writer = csv.writer(output, delimiter = ',')

    all = []
    row = next(reader)
    row.insert(5, 'Results')
    all.append(row)

    for row in reader:
        for i in final_results:
            print type(i)
            row.insert(5, i)
        all.append(row)
    writer.writerows(all)
</code></pre>

<p><strong>Results for Method 1</strong></p>

<pre><code>Type,Id,TypeId,CalcValues
B,111K,111Kequity(long) 111K,116.211768
C,111N,B(long) 111N,0.106559957
B,111J,c(long) 111J,20.0616340.1065599566767107
0.0038113334533441123
20.061623176440904
</code></pre>

<p><strong>Results for Method 2</strong></p>

<pre><code>Type,Id,TypeId,CalcValues,Results
B,111K,111Kequity(long) 111K,116.211768,0.1065599566767107,20.061623176440904,0.0038113334533441123
C,111N,B(long) 111N,0.106559957,0.1065599566767107,20.061623176440904,0.0038113334533441123
B,111J,c(long) 111J,20.061634,0.1065599566767107,20.061623176440904,0.0038113334533441123
</code></pre>

<p><strong>Expected Result</strong></p>

<pre><code>Type,Id,TypeId,CalcValues,ID
B,111K,111Kequity(long) 111K,116.211768,0.1065599566767107
C,111N,B(long) 111N,0.106559957,20.061623176440904
B,111J,c(long) 111J,20.061634,0.0038113334533441123
</code></pre>

<pre><code>import csv

HEADER = "Type,Id,TypeId,CalcValues,ID"
final_results = ['0.1065599566767107', '20.061623176440904', '0.0038113334533441123']

with open("test.csv") as inputs, open("tmp.csv", "wb") as outputs:
    reader = csv.reader(inputs, delimiter=",")
    writer = csv.writer(outputs, delimiter=",")

    reader.next()  # ignore header line
    writer.writerow(HEADER.split(","))  

    for row in reader:
        writer.writerow(row + [final_results.pop(0)])
</code></pre>

<p>I store the header fields into <code>HEADER</code> and switch 2nd and 3rd elements of <code>final_results</code>, use <code>pop(0)</code> to remove and return the first element of <code>final_results</code></p>

<p>output:</p>

<pre><code>Type,Id,TypeId,CalcValues,ID
B,111K,111Kequity(long) 111K,116.211768,0.1065599566767107
C,111N,B(long) 111N,0.106559957,20.061623176440904
B,111J,c(long) 111J,20.061634,0.0038113334533441123
</code></pre>

python list loops csv parsing