I am trying to find the MIN and MAX values for each row of a CSV file and append them to the next position in the list, positions 5 and 6. I have managed to calculate the averag
import csv
sample = open("sampleData.txt", "r")
csv1 = csv.reader(sample, delimiter = ',')
sorted_list = []
for line in csv1:
print '-- ORIG:', line
tmp = sorted( [int(i) for i in line[1:]], reverse=True ) # eg: [99,78,56]
stat_list = [round(sum(tmp)/float(len(tmp)), 2), min(tmp), max(tmp)]
sorted_list.append( [line[0]] + tmp + stat_list )
for s in sorted_list: print '** NEW: ', s # has ['Fred',99,78,56,78.0,57,99]
You can use/modify the quick & dirty solution above. Note:
ORIG: ['Bar', '23', '88', '65']
NEW: ['Fred', 99, 78, 57, 78.0, 57, 99]