I am trying to create a update function in order to update student marks value in the same csv file when the or studentID matches that is queried by the user; My csv file looks like following;
Name, studentID, Marks, Grade
Joan, 1735, 90, A
Dev, 0374, 69, B
Betty, 647, 55, C
My tried code is:
def updatefunc():
stid= input("Enter a studentID : ")
fieldnames = ["Name", "StuID", 'Marks', 'Grade']
with open('File.csv', 'r') as csvfile, open('output.csv', 'w') as output:
reader = csv.DictReader(csvfile, fieldnames=fieldnames)
writer = csv.DictWriter(output, fieldnames=fieldnames)
for row in reader:
if chid == row['ID']:
row['Marks'] = input("enter new name {}".format(Marks))
writer.writerow({'Name': row['Name'], 'StuID': row['StuID'], 'Marks': row['Marks'],'Grade': row['Grade']})