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>

<p>First method is bound to fail: you don't want to add new lines but new columns. So back to second method:</p>

<p>You insert the title OK, but then you're looping through the results on each row, whereas you need to iterate on them.</p>

<p>For this, i create an iterator from the <code>final_results</code> list (with <code>__iter__()</code>), then I call <code>it.next</code> and append to each row (no need to insert in the end, just append)  </p>

<p>I removed the <code>all</code> big list, because 1) you can write one line at a time, saves memory, and 2) <code>all</code> is a predefined function. Avoid to use that as a variable.</p>

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

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


    row = next(reader)  # read title line
    row.append("Results")
    writer.writerow(row)  # write enhanced title line

    it = final_results.__iter__()  # create an iterator on the result

    for row in reader:
        if row:  # avoid empty lines that usually lurk undetected at the end of the files
            try:
                row.append(next(it))  # add a result to current row
            except StopIteration:
                row.append("N/A")     # not enough results: pad with N/A
            writer.writerow(row)
</code></pre>

<p>result:</p>

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

<p>Note: had we included <code>"Results"</code> in the <code>final_results</code> variable, we wouldn't even have needed to process first line differently.</p>

<p>Note2: the values seem wrong: <code>final_results</code> seems not in the same order as the expected output. And the <code>Result</code> column has turned to <code>ID</code>, but that's easy to correct.</p>

python list loops csv parsing