ss Average: Reading Student Records from a CSV File) Use Python Use the csv module to read the grades.csv file from the previous exerci
(Class Average: Reading Student Records from a CSV File) Use Python
Use the csv module to read the grades.csv file from the previous exercise (exercise 9.3).
Display the data in tabular format, including an additional column showing each student’s average to the right of that student’s three exam grades and an additional row showing the class average on each exam below that exam’s column.
This is exercise 9.3
# Importing csv module
import csv
# empty list to store data
data = []
columns = ["firstname", "lastname", "grade1", "grade2", "grade3"]
filename = "grades.csv"
for i in range(3):
firstname = input("Enter First Name : ")
lastname = input("Enter Last Name : ")
grade1 = float(input("Enter Grade 1 : "))
grade2 = float(input("Enter Grade 2 : "))
grade3 = float(input("Enter Grade 3 : "))
data.append([firstname, lastname, grade1, grade2, grade3])
print()
# write data and columns as csv file
with open(filename, 'w') as csvfile:
# creating a csv writer object
csvwriter = csv.writer(csvfile)
# writing the columns
csvwriter.writerow(columns)
# writing the each row to the data
csvwriter.writerows(data)
print("CSV file {} has been created successfully!!".format(filename))
Trending now
This is a popular solution!
Step by step
Solved in 2 steps