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.
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.
I have included my code below. Appreciate any input or guidance.
Existing CSV Test.csv
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 - Method 1 & 2
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)
Results for Method 1
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
Results for Method 2
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
Expected Result
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
Resultcolumn has turned toID. Can you edit it?