Merge .csv columns with python issue

I need to add one column of data from one .csv file to another. The script below does just that however, the columns data is from one row. I’m not sure how I should properly loop through the column. The columns are frame, x, y, z variations written by another script from motion tacking data.

First file:


1 , -0.0004601776599884033 , 0.00011354684829711914, 0.0
2 , -6.380677223205566e-05 , -0.017401933670043945, 0.0
3 , 0.0029741227626800537 , -0.023329734802246094, 0.0

Second file:


2 , -0.005525648593902588 , 0.005231067538261414, 0.0,
3 , -0.008330583572387695 , 0.004555210471153259, 0.0,
4 , -0.010956943035125732 , 0.002620011568069458, 0.0,

Column 1,2,3 are good but the 4th is just “-0.02447640895843506” which is the last row of third column from the first file.
Output file data:


2 , -0.005525648593902588 , 0.005231067538261414, -0.02447640895843506 
3 , -0.008330583572387695 , 0.004555210471153259, -0.02447640895843506
4 , -0.010956943035125732 , 0.002620011568069458, -0.02447640895843506

Column 3 needs to be:


2 , -0.005525648593902588 , 0.005231067538261414, 0.00011354684829711914
3 , -0.008330583572387695 , 0.004555210471153259, -0.017401933670043945
4 , -0.010956943035125732 , 0.002620011568069458, -0.023329734802246094 

import os
import csv


file = os.path.join(os.getcwd(), 'data', 'Track.chest.2.csv')#random files from the data set
file2 = os.path.join(os.getcwd(), 'data', 'Track.shin.L.csv')
outputfile = os.path.join(os.getcwd(), 'data', 'Test.csv')




TestLoadAndMergeDataSet1 = 1




def read_csv(TestLoadAndMergeDataSet1):
     output = [] #frame number x y + zcorrdsdata
     zcorrdsdata = []#z data from file2
     f = open(file, 'rU') #open the file in read universal mode
     f2 = open(file2, 'rU') #open the file in read universal mode
     try: 
         for line in f:
             cells = line.split( "," )
             zcorrdsdata.append( ( cells[ 0 ], cells[ 1 ], cells[ 2 ] ) ) #since we want the first, second and third column
         for line in f2:
             cellsx = line.split( "," )
             #cellsx[3] = cells[2]
             output.append((cellsx[0],cellsx[1],cellsx[2],cells[2]))#first, second, third and fourth column
             fn = open(outputfile, 'r+' ) 
             writer = csv.writer(fn)
             writer.writerows(output)#print(output) 
     finally:
        f.close()
        f2.close()
        fn.close()
        print("files were closed")#close files to prevent memory leaks 


read_csv(TestLoadAndMergeDataSet1)    

Not really Blender related, but anyway. Here’s a really straightforward solution:

f1 = """1 , -0.0004601776599884033 , 0.00011354684829711914, 0.0
2 , -6.380677223205566e-05 , -0.017401933670043945, 0.0
3 , 0.0029741227626800537 , -0.023329734802246094, 0.0"""

f2 = """2 , -0.005525648593902588 , 0.005231067538261414, 0.0,
3 , -0.008330583572387695 , 0.004555210471153259, 0.0,
4 , -0.010956943035125732 , 0.002620011568069458, 0.0,"""

for l1, l2 in zip(f1.splitlines(), f2.splitlines()):
    tmp = l2.split(",")[:3]
    tmp.append(l1.split(",")[2])
    print(",".join(tmp))