How do I make a new concatenated column?
<p>I have a .csv that has data like this:</p>

<pre><code>30,972.74176,0.446378,1.825426,cl+l  
31,1015.075579,0.279686,2.950118,l  
32,1072.59334,0.273239,4.408458,cl+l  
33,1139.55317,-1.616904,4.756378,l  
34,1279.86304,50.335186,3.877577,r  
35,1181.541419,61.399389,5.155133,r  
36,1097.421199,63.809733,5.37508,r  
37,1018.555678,64.034432,2.59232,r
</code></pre>

<p>I have written a program that takes out the rows where the last column (E, as I am calling it in my program) is either an "l" or "cl+l" and copies it to another file called "left.csv".  What I want to do is add another column to left.csv that takes the data from the second column (B), put a comma in between them, and then the data from column D.  </p>

<p>So, left.csv right now looks like:</p>

<pre><code>30,972.74176,0.446378,1.825426,cl+l  
31,1015.075579,0.279686,2.950118,l  
32,1072.59334,0.273239,4.408458,cl+l  
33,1139.55317,-1.616904,4.756378,l
</code></pre>

<p>What I want is:</p>

<pre><code>30,972.74176,0.446378,1.825426,cl+l,"972.74176,1.825426"  
31,1015.075579,0.279686,2.950118,l,"1015.075579,2.950118"    
32,1072.59334,0.273239,4.408458,cl+l,"1072.59334,4.408458"   
33,1139.55317,-1.616904,4.756378,l,"1139.55317,4.756378"
</code></pre>

<p>How should I go about achieving this, and how can I tell the program that the comma should not start the next entry even though it's csv?  Is that possible, and will I have to have the parentheses or a different delimiter maybe?  The code I have right now is:</p>

<pre><code>import csv
import sys
import operator

fieldnames = ["A","B","C","D","E"]
surveyfile = open("source.csv", "r")
left_file = open("left.csv",'wb')
right_file = open("right.csv",'wb')
coil_file = open("coil1.csv","wb")


reader = csv.DictReader(surveyfile, fieldnames=fieldnames, delimiter=",")
left_writer = csv.DictWriter(left_file, fieldnames, delimiter=",")
sortedlefts = sorted(reader,key=lambda x:float(x["B"]))

surveyfile.seek(0,0)

right_writer = csv.DictWriter(right_file, fieldnames, delimiter=",")
sortedrights = sorted(reader,key=lambda x:float(x["B"]), reverse=True)

coil_writer = csv.DictWriter(coil_file, fieldnames, delimiter=",")

for row in sortedlefts:
    if row["E"] == "l" or row["E"] == "cl+l":
        left_writer.writerow(row)
        coil_writer.writerow(row)   


for row in sortedrights:
    if row["E"] == "r":
        right_writer.writerow(row)
        coil_writer.writerow(row)
</code></pre>
