I want writing function for sorted_average() . I calculated average of data in input file and write them in my output file. then i want sort data of out put file. but my cod not working...
my out put file is :
- mandana,7.5
- hamid,6.066666666666666
- sina,11.285714285714286
- sara,9.75
- soheila,7.833333333333333
- ali,5.0
- sarvin,11.375
I want sorting this numbers....can help me?
def calculate_sorted_averages(input_file_name, output_file_name):
labels = ['name', 'mean_grade']
with open(input_file_name, 'r') as file_obj:
csv_obj = csv.reader(file_obj)
data = [
[row[0], statistics.mean(float(grade) for grade in row[1:])]
for row in csv_obj]
# sort data according to `name`
sorted(data, key=lambda x: x[0])
with open(output_file_name, 'w') as file_obj:
obj = csv.writer(file_obj)
csv_obj.writerow(labels)
for row in data:
csv_obj.writerow(row)
return file_obj
calculate_sorted_averages('input.csv','output.csv')
0 Answer(s)