Skip to main content

CSV SAMPLE PROJECT | MENU DRIVEN | 14 AUG

 

# -*- coding: utf-8 -*-

"""

Created on Thu Aug 14 13:21:08 2025


@author: Kirti Hora

"""

import csv

def createcsv():

    filename = "emp.csv"

    with open(filename, "w", newline="") as csv_fobj:

        wtr_obj = csv.writer(csv_fobj)

        wtr_obj.writerow(["Empno", "Name", "Basic Salary", "Allowance", "Deduction", "Net Salary"])

        while True:

            eno = input("Enter employee no: ")

            name = input("Enter name: ")

            bs = float(input("Enter basic salary: "))

            allowance = 2000

            dedn = bs * 0.05

            net = bs + allowance - dedn

            wtr_obj.writerow([eno, name, bs, allowance, dedn, net])

            ans = input("Want to enter another (y/n)? ").lower()

            if ans == "n":

                break

    print("CSV file created successfully!")


def displaycsv():  # UDF to display all records

    with open("emp.csv", "r") as csv_fobj:

        rdrobj = csv.reader(csv_fobj)

        for rec in rdrobj:

            print(rec)


def searcheno():  # UDF to search employee no

    with open("emp.csv", "r") as csv_fobj:

        eno = input("Enter Employee no to search: ")

        rdrobj = csv.reader(csv_fobj)

        found = False

        for rec in rdrobj:

            if rec[0] == eno:

                print(rec)

                found = True

                break

        if not found:

            print("Record not found")


def countall():  # UDF to count total records

    csv_fobj = open("emp.csv", "r")

    rdrobj = csv.reader(csv_fobj)

    totrec = 0

    for rec in rdrobj:

        totrec += 1

    csv_fobj.close()

    print("Total records -", totrec - 1)  # one record deducted for heading


def searchname():  # UDF to search on name

    with open("emp.csv", "r") as csv_fobj:

        nm = input("Enter Employee name to search: ")

        rdrobj = csv.reader(csv_fobj)

        ctr = 0

        for rec in rdrobj:

            if rec[1] == nm:

                print(rec)

                ctr += 1

        if ctr == 0:

            print("Record not found")


# Menu-driven program

ans = 'y'

while ans == 'y':

    print("\n1. Create the Employee CSV file")

    print("2. Display the details")

    print("3. Count total records")

    print("4. Search on basis of Employee no")

    print("5. Search on Employee name")


    ch = int(input("\nEnter choice: "))

    if ch == 1:

        createcsv()

    elif ch == 2:

        displaycsv()

    elif ch == 3:

        countall()

    elif ch == 4:

        searcheno()

    elif ch == 5:

        searchname()

    else:

        print("Wrong choice")


    ans = input("Want to continue (y/n)? ").lower()


Que 2: Write code to :

·       Stores Admno, Stud_Name, Marks, Percen in student.csv.

·       Allows searching records where Percen > 75.

 Sol:

import csv

# Function to add student data

def add_students():

    with open("student.csv", "w", newline="") as f:

        writer = csv.writer(f)

        writer.writerow(["Admno", "Stud_Name", "Marks", "Percen"])

       

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

        for i in range(n):

            admno = input("Enter Admission No: ")

            name = input("Enter Student Name: ")

            marks = int(input("Enter Marks out of 100: "))

            percen = marks   # since marks out of 100 → percentage = marks

            writer.writerow([admno, name, marks, percen])

    print("Data written successfully!")

 

# Function to search students with percentage > 75

def search_students():

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

        reader = csv.reader(f)

        next(reader)  # skip header row

        print("\nStudents with Percentage > 75:")

        found = False

        for row in reader:

            if int(row[3]) > 75:

                print(row)

                found = True

        if not found:

            print("No student found with >75%")

 

# Main Program

add_students()

search_students()


Comments

Popular posts from this blog

PYTHON - MYSQL CONNECTIVITY CODE

  #INSERTION OF DATA import mysql.connector mydb = mysql.connector.connect( host="localhost", user="root", passwd="root", database="school" ) print("Successfully Connected") #print(mydb) mycursor=mydb.cursor()   v1=int(input("enter ID:")) v2=input("enter name:") v3=input("enter Gender:") v4=int(input("enter age:")) sql='insert into TEACH values("%d","%s","%s","%s")'%(v1,v2,v3,v4) print(sql) mycursor.execute(sql) mydb.commit() print("record added") #MYSQL Connection code – Deletion on database SOURCE CODE: s=int(input("enter id of TEACHER to be deleted:")) r=(s,) v="delete from TEACH where id=%s" mycursor.execute(v,r) mydb.commit() print("record deleted") MYSQL Connection code – Updation on database SOURCE CODE: import mysql.connector mydb = mysql.connector.c...

GRADE XII CS - VIVA QUESTIONS

  VIVA QUESTIONS GRADE XII CS Dear All Be thorough with your project and practical files, as the viva can be asked from anywhere. Stay calm, don’t get nervous, and be confident in front of the examiner. 1. Tell me about your project. 2. Which concepts you have used for your project? 3. What do you mean by front end and back end? How they are important in developing any such projects? 4  Mention the modules and built-in functions you have used in your project. 5. Which real world problems are solved by your project? 6. Explain the most important feature of your project. 7. Name a few mutable data types of python. Lists, Sets, and Dictionaries 8. Name a few immutable data types of python. Strings, Tuples, Numeric 9. Name ordered and unordered data type of python. Ordered – String, List, Tuples Unordred – Set, Dictionaries 10. What is the significance of a pass statement in python? pass is no operation python statement. This is used where python requires syntax but logic requires...