import csv
# Data rows as dictionary objects
d = [
{'name': 'Nikhil', 'branch': 'COE', 'year': '2', 'cgpa': '9.0'},
{'name': 'Sanchit', 'branch': 'COE', 'year': '2', 'cgpa': '9.1'},
{'name': 'Aditya', 'branch': 'IT', 'year': '2', 'cgpa': '9.3'},
{'name': 'Sagar', 'branch': 'SE', 'year': '1', 'cgpa': '9.5'},
{'name': 'Prateek', 'branch': 'MCE', 'year': '3', 'cgpa': '7.8'},
{'name': 'Sahil', 'branch': 'EP', 'year': '2', 'cgpa': '9.1'}
]
# Field names
f = ['Name', 'Branch', 'Year', 'CGPA']
# Name of the CSV file
fn = "university_records.csv"
# Writing to CSV file
with open(fn, 'w', newline='') as csvfile:
# Creating a CSV DictWriter object
writer = csv.DictWriter(csvfile, fieldnames=f)
# Writing headers (field names)
writer.writeheader()
# Writing data rows
writer.writerows(d)