Thursday, 29 May 2025

GRADE 12 - PRACTICAL 11 - BINARY FILES

"""WAPP to demonstrate the following optionsby creating different methods using binary files for a hospital management application :

(i) adding records of the structure :  {'pid':<str>,'pname':<str>,'diag':<str>,'fees':<int>}

(ii)reading and displaying all the records in a

tabular format

(iii)searching and displaying patients who have 'corona'

(iv)update the diagnosis of the given patient id

(Display appropriate messages if patient not found or file not found.)

"""



import pickle def menu(): while True: print("""Menu 1)Enter patient details 2)Display patient details 3)Search 4)Update 5)Exit'""") ch = int(input("Enter your choice : ")) if ch==1: accept() elif ch==2: display() elif ch==3: search() elif ch==4: update() else: break def accept(): with open('Diagnostics.dat','ab') as f: pid=input('Enter patient id : ') pname=input(("Enter patient's name : ")) diag = input('Enter patient diagnosis : ') fee = int(input('Enter patient fee :')) d={'pid':pid,'pname':pname,'diag':diag,'fee':fee} pickle.dump(d,f) def display(): try: with open('Diagnostics.dat','rb') as f: print('pid'.ljust(15),'pname'.ljust(20),'diagnosis'.ljust(15),'fees') while True: rec = pickle.load(f) print(rec['pid'].ljust(15),rec['pname'].ljust(20),rec['diag'].ljust(15),rec['fees']) except FileNotFoundError: print('Sorry, File not found') except EOFError: pass except Exception as e: print("Unexpected error : ",e) def search(): try: with open('Diagnostics.dat','rb') as f: print("Patient details with corona : ") while True: rec = pickle.load(f) if rec[2] == "corona": for i in rec: print(str(i).ljust(15),end='') print() except FileNotFoundError: print('Sorry, File not found') except EOFError: pass except Exception as e: print("Unexpected error : ",e) def update(): try: with open('Diagnostics.dat','rb') as f: pid=input("Enter pid of patient to be updated : ") l=[] found=False while True: rec = pickle.load(f) if rec[0] == pid: rec[2]=input("Enter new diagnosis") found=True print("Record updated successfully") l.append(rec) if found== False: print("Required record not found") with open('Diagnostics.dat','wb') as f: for rec in l: pickle.dump(rec,f) except FileNotFoundError: print('Sorry, File not found') except EOFError: pass except Exception as e: print("Unexpected error : ",e) menu()

Wednesday, 21 May 2025

GRADE 12: PRACTICAL 10: CSV FILES

''' Write a menu-driven program to perform the following

functions on a csv file ‘Cricket.csv’

i) Accept details of n players and write into the file.

The header will be:  [Player ID, Player Name, Team, Runs Scored]

ii) Display the id, names and runs of all players who have scored

century. Also display the average of such players.

iii) Search for a player by name and display their details 

if found, else show "Player not found."

iv) Update the runs scored by a player (given their Player ID)

If found, ask for new runs and update the file.

'''

import csv


def menu():

    while True :

        ch=int(input("""MENU :

1.Accept

2.Display

3.Search

4.Update

5.Exit

"""))

        if ch==1:

            accept()

        elif ch==2:

            display()

        elif ch==3:

            search()

        elif ch==4:

            update()

        else:

            break


def accept():

    n=int(input("Enter no of records : "))

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

        writer=csv.writer(f)

        if f.tell()==0:

            writer.writerow(['Player ID', 'Player Name',' Team', 'Runs Scored'])

        for i in range(n):

            pid = input("Enter Player ID: ")

            name = input("Enter Player Name: ")

            team = input("Enter Team Name: ")

            runs = int(input("Enter Runs Scored: "))

            writer.writerow([pid, name, team, runs])


def display():

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

        reader = csv.reader(f)

        title=next(reader)

        high=[]

        print('ID'.ljust(15),'NAME'.ljust(15),'RUNS'.ljust(15))

        for a,b,c,d in reader:

            if int(d)>=100:

                high.append(int(d))    

                print(a.ljust(15),b.ljust(15),d.ljust(15))

        if len(high)==0:

            print( 'No such records')

        else:

            print('Average of the top batsmen : ', sum(high)/len(high))

            

                  

def search():

    name=input("Enter player name to be searched : ")

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

        reader=csv.reader(f)

        header=next(reader)

        for i in reader:

            if i[1]==name:

                print("Player found : ",i)

                break

        else:

            print("Player not found ")


def update():

    with open('Cricket.csv','r') as f:

        c=csv.reader(f)

        l=list(c)

        pid=input("enter player id to be updated")

        for i in l[1:]:

            if pid==i[0]:

                i[3]=int(input("Enter new no of runs"))

                break

        else:

            print("player not found")

    with open('Cricket.csv','w',newline='') as f:

        c=csv.writer(f)

        c.writerows(l)

        


menu()