0

How to loop on a csvfile

hello I have this question Open student_record.csv For each row: calculate the final_grade by taking the mean of Q1, Q2, Q3 and Q4 for the remarks of the final grade, use the following criteria: FAILING - final_grade < 60 CONDITIONAL - final_grade >= 60 and final_grade < 70 PASSING - final_grade >= 70 Save resulting rows into student_record_graded.csv Now stuck anyone to help me import csv with open('student_record.csv','w+') as csvfile: writer = csv.writer(csvfile) writer.writerow(['Firstname','Lastname','Q1','Q2','Q3','Q4']) writer.writerow(['student_1','surname_1',50,60,90,70]) writer.writerow(['student_2','surname_2',50,60,90,70]) writer.writerow(['student_n','surname_n',80,75,60,88]) with open('student_record.csv','r') as csvfile: reader = csv.DictReader(csvfile) for row in reader: print(row) with open('student_record.csv','r') as csvfile: reader = csv.DictReader(csvfile) def mean(values): length = len(values) total_sum = 0 for i in range(length): total_sum += values[i] total_sum = sum(values) average = total_sum * 1.0/length return average student_1 = [50,60,90,70] n = mean(student_1) print("Student_1 Final_grade =", n) student_2 = [50,60,90,70] x = mean(student_2) print("Student_2 Final_grade =", x) student_n =[80,75,60,88] y = mean(student_n) print("Student_n final_grade =", y) reader = open("student_record.csv","r") writer = open('student_record_graded.csv','w+') csv_reader = csv.DictReader(reader) columns = ['Firstname','Lastname','Q1','Q2','Q3','Q4','Final_grade','Remark'] csv_writer = csv.DictWriter(writer,fieldnames=columns) csv_writer.writeheader() for row in csv_reader: print("Row before evaluation:" , row) Final_grade = 0 if Final_grade >= 70: row["Remark"] = "Pass" elif Final_grade >= 60 and Final_grade <70: row["Remark"] = 'conditional' elif Final_grade < 60: row["Remark"] = 'Failing' print("Row after evaluatio

10th Jul 2019, 7:17 PM
Faith Tinarwo
Faith Tinarwo - avatar
4 Antworten
+ 1
Please paste your code in the Code Playground and share the link here.
10th Jul 2019, 7:36 PM
Diego
Diego - avatar
10th Jul 2019, 8:04 PM
Faith Tinarwo
Faith Tinarwo - avatar
+ 1
1. Change line 66 to Final_grade = mean([int(row["Q1"]), int(row["Q2"]), int(row["Q3"]), int(row["Q4"])]) 2. Change line 76 to print("Row after evaluation", row)
13th Jul 2019, 5:00 AM
Diego
Diego - avatar
+ 1
Diego thank you so much👏🏼👏🏼
14th Jul 2019, 6:32 PM
Faith Tinarwo
Faith Tinarwo - avatar