Previous Index Next

Solved Examples - Pickle dictionary object

Question 1

Write a menu driven program in Python that asks the user to add, display, and search records of students stored in a binary file. The student record contains roll no, name and test score. It should be stored in a dictionary object. Your program should pickle the object and save it to a binary file.

Program (student-database.py)

import pickle

def set_data():
    rollno = int(input('Enter roll number: '))
    name = input('Enter name: ')
    test_score = int(input('Enter test score: '))
    print()
    
    #create a dictionary
    student = {}
    student['rollno'] = rollno
    student['name'] = name
    student['test_score'] = test_score
    
    return student


def display_data(student):
    print('Roll number:', student['rollno'])
    print('Name:', student['name'])
    print('Test Score:', student['test_score'])
    print()


def write_record():
    #open file in binary mode for writing.
    outfile = open('student.dat', 'ab')

    #serialize the object and writing to file
    pickle.dump(set_data(), outfile)

    #close the file
    outfile.close()


def read_records():
    #open file in binary mode for reading
    infile = open('student.dat', 'rb')

    #read to the end of file.
    while True:
        try:
            #reading the oject from file
            student = pickle.load(infile)

            #display the object
            display_data(student)
        except EOFError:
            break

    #close the file
    infile.close()

def search_record():
    infile = open('student.dat', 'rb')
    rollno = int(input('Enter rollno to search: '))
    flag = False
    
    #read to the end of file.
    while True:
        try:
            #reading the oject from file
            student = pickle.load(infile)

            #display record if found and set flag
            if student['rollno'] == rollno:
                display_data(student)
                flag = True
                break
            
        except EOFError:
            break

    if flag == False:
        print('Record not Found')
        print()
        
    #close the file
    infile.close()

def show_choices():
    print('Menu')
    print('1. Add Record')
    print('2. Display Records')
    print('3. Search a Record')
    print('4. Exit')

def main():
    while(True):
        show_choices()
        choice = input('Enter choice(1-4): ')
        print()
        
        if choice == '1':
            write_record()
            
        elif choice == '2':
            read_records()

        elif choice == '3':
            search_record()

        elif choice == '4':
            break
        
        else:
            print('Invalid input')
            
#call the main function.
main()

Output

Menu
1. Add Record
2. Display Records
3. Search a Record
4. Exit
Enter choice(1-4): 1

Enter roll number: 21
Enter name: Deepak
Enter test score: 78

Menu
1. Add Record
2. Display Records
3. Search a Record
4. Exit
Enter choice(1-4): 1

Enter roll number: 22
Enter name: Saksham
Enter test score: 90

Menu
1. Add Record
2. Display Records
3. Search a Record
4. Exit
Enter choice(1-4): 2

Roll number: 21
Name: Deepak
Test Score: 78

Roll number: 22
Name: Saksham
Test Score: 90

Menu
1. Add Record
2. Display Records
3. Search a Record
4. Exit
Enter choice(1-4): 3

Enter rollno to search: 10
Record not Found

Menu
1. Add Record
2. Display Records
3. Search a Record
4. Exit
Enter choice(1-4): 3

Enter rollno to search: 22
Roll number: 22
Name: Saksham
Test Score: 90

Menu
1. Add Record
2. Display Records
3. Search a Record
4. Exit
Enter choice(1-4): 4

>>> 

Previous Index Next