Wednesday, 14 January 2026

CSV FILE - > USE METHOD 2 (SKIP HEADER IF YOU FIND THAT TOUGH) OR METHOD 1 (LIL MORE DETAILED)

 METHOD 1 : 

import csv


# Menu function

def menu():

    while True:

        print("\n1. Write Student Records")

        print("2. Display Student Records")

        print("3. Calculate Average Marks")

        print("4. Search Student by ID")

        print("5. EXIT")


        choice = int(input("Enter your choice: "))


        if choice == 1:

            write_students()

        elif choice == 2:

            display_students()

        elif choice == 3:

            average_marks()

        elif choice == 4:

            search_student()

        elif choice == 5:

            print("Exiting program.")

            break

        else:

            print("Invalid choice")


# i) Write student records (append mode + header check)

def write_students():

    with open("Students.csv", "a", newline="") as f:

        writer = csv.writer(f)

        if f.seek(0)==0:

            writer.writerow(["Student ID", "Student Name", "Class", "Marks"])


        n = int(input("Enter number of students: "))

        for i in range(n):

            sid = input("Student ID: ")

            name = input("Student Name: ")

            clas = input("Class: ")

            marks = float(input("Marks: "))

            writer.writerow([sid, name, clas, marks])


    print("Records written successfully")


# ii) Display contents of CSV file

def display_students():

    try:

        with open("Students.csv", "r") as f:

            reader = csv.reader(f)

            print("\nStudent Records:")

            for row in reader:

                print(row)

    except FileNotFoundError:

        print("Error: File not found")


# iii) Calculate average marks (skip header)

def average_marks():

    try:

        with open("Students.csv", "r") as f:

            c = csv.reader(f)

            next(c)  # skip header

            total = count = 0

            for row in c:

                total += int(row[3])

                count += 1


        print("Average Marks =", total / count)

        

    except FileNotFoundError:

        print("Error: File not found")


# iv) Search student using Student ID (skip header)

def search_student():

    try:

        with open("Students.csv", "r") as f:

            reader = csv.reader(f)

            next(reader)  # skip header

            sid = input("Enter Student ID to search: ")

            found = False

            for row in reader:

                if row[0] == sid:

                    print("Student Found:", row)

                    found = True

                    break


        if not found:

            print("Student not found")


    except FileNotFoundError:

        print("Error: File not found")


# Call menu

menu()




METHOD 2:

import csv

# Menu function
def menu():
    while True:
        print("\n1. Write Student Records")
        print("2. Display Student Records")
        print("3. Calculate Average Marks")
        print("4. Search Student by ID")
        print("5. EXIT")

        choice = int(input("Enter your choice: "))

        if choice == 1:
            write_students()

        elif choice == 2:
            display_students()

        elif choice == 3:
            average_marks()

        elif choice == 4:
            search_student()

        elif choice == 5:
            print("Exiting program.")
            break

        else:
            print("Invalid choice")

# i) Accept details and write into CSV file
def write_students():
    with open("Students.csv", "a", newline="") as f:
        writer = csv.writer(f)
        n = int(input("Enter number of students: "))
        for i in range(n):
            sid = input("Student ID: ")
            name = input("Student Name: ")
            clas = input("Class: ")
            marks = float(input("Marks: "))
            writer.writerow([sid, name, clas, marks])
    print("Records written successfully")

# ii) Display contents of CSV file
def display_students():
    with open("Students.csv", "r") as f:
        reader = csv.reader(f)
        print("\nStudent Records:")
        for row in reader:
            print(row)

# iii) Calculate average marks
def average_marks():
    with open("Students.csv", "r") as f:
        reader = csv.reader(f)
        total = count = 0
        for row in reader:
            total += float(row[3])
            count += 1

    print("Average Marks =", total / count)
    

# iv) Search student using Student ID
def search_student():
    with open("Students.csv", "r") as f:
        reader = csv.reader(f)
        sid = input("Enter Student ID to search: ")
        found = False
        for row in reader:
            if row[0] == sid:
                print("Student Found:", row)
                found = True
                break

    if not found:
        print("Student not found")


# Call menu function
menu()

No comments:

Post a Comment