Q. Considering the following definition of dictionary MULTIPLEX, write a method in python to search and display all the content in a pickled file CINEMA.DAT, where MTYPE key of the dictionary is matching with the value "Comedy".


MULTIPLEX = {'MNO': _____, "MNAME": _____, 'MTYPE': _____}

Answer :-

import pickle
def Search():
    file = open('CINEMA.DAT', "rb")
    try:
        while True:
            MULTIPLEX = pickle.load(file)
            if MULTIPLEX ["MTYPE"] == "Comedy":
                print (MULTIPLEX)
    except EOFError:
        file.close()

Search()

For Output, we have to write something in the CINEMA.dat file. 

Code for writing :- 

import pickle

f  = open("CINEMA.DAT","wb")
             
while True :
      mno = int(input ("Enter MNO ( for exit enter -1 ) :- "))
      if mno == -1 :
            break
      mname = input ("Enter MNAME :- ")
      mtype = input ("Enter MTYPE :- ")
      multi = { 'MNO': mno , "MNAME": mname , 'MTYPE': mtype }
      pickle.dump( multi ,f )
print("Thankyou")
f.close()

Output for writing :-

Enter MNO ( for exit enter -1 ) :- 123
Enter MNAME :- Path
Enter MTYPE :- Web
Enter MNO ( for exit enter -1 ) :- 456
Enter MNAME :- Walla
Enter MTYPE :- site
Enter MNO ( for exit enter -1 ) :- 789
Enter MNAME :- Portal
Enter MTYPE :- Comedy
Enter MNO ( for exit enter -1 ) :- 159
Enter MNAME :- Express
Enter MTYPE :- Comedy
Enter MNO ( for exit enter -1 ) :- 753
Enter MNAME :- Python
Enter MTYPE :- programming
Enter MNO ( for exit enter -1 ) :- -1
Thankyou
>>>

Final Output :-

{'MNO': 789, 'MNAME': 'Portal', 'MTYPE': 'Comedy'}
{'MNO': 159, 'MNAME': 'Express', 'MTYPE': 'Comedy'}
>>>

2 Comments

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post