每当我退出程序并再次尝试添加时,我的代码总是重新编写标题行。我该如何解决这个问题?
import csv
def add_student():
print("*****************************************")
print(" ADD STUDENT INFORMATION ")
print("*****************************************")
with open('student.csv', 'a', newline='') as file:
student_writer = csv.writer(file)
id_number = input("Enter Student ID Number: ")
name = input("Enter Student Name: ")
year = input("Enter Student Year: ")
course = input("Enter Student Course: ")
department = input("Enter Student Department: ")
student_writer.writerow([id_number, name, year, course, department])
print("Student Information added successfully")
input("Press Enter to Continue...")
def view_all_students():
with open('student.csv', 'r') as file:
student_data = csv.reader(file)
for row in student_data:
print(row)
input("Press Enter to Continue...")
# Write the header row to the file
with open('student.csv', 'a', newline='') as file:
student_writer = csv.writer(file)
student_writer.writerow(["ID_Number", "Name", "Year", "Course", "Department"])
while True:
display_menu()
choice = input("Enter your choice: ")
if choice == '1':
add_student()
elif choice == '2':
view_all_students()
else:
print("Invalid choice. Please try again.")
如何修复只显示一次标题行的代码?