Q. Considering the following definition of dictionary COMPANY, Write a method in Python to search and display the content in a pickled file COMPANY.DAT, where CompID key of the dictionary is matching with the value '1005'.

 

Company = {'CompID' = ____, 'CName' = ____, ‘Turnover’ = ____}

 

Answer =


import pickle

file = open("COMPANY.DAT","rb")
found = 0
try :
      while True :
            Company = pickle.load(file)
            if Company [ "CompID" ] == '1005':
                  print(Company)
                  found=1
            
except EOFError :
      if found == 0:
            print("Not found !!!")     

file.close()



7 Comments

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

  1. your answers are really vey helpful and you also provide explaination of these questions...so thank you

    ReplyDelete
  2. Showing file not found

    ReplyDelete
  3. can I have the output

    ReplyDelete

Post a Comment

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

Previous Post Next Post