Name of Experiment: Prog to create a binary file and search a particular record
Q11. Write a program to create a binary file student.dat, which contains some students’
   Roll and Name. Now read the file and search for a given Roll number and display
   the Name, if not found display appropriate message.
Program:
     import pickle
        # Creating binary File
     File = open("student.dat", "wb")
     stud = dict()
     ch = 'y'
     while ch == 'y' :
         roll = int(input(" Enter Roll Number: "))
         name = input(" Enter Name: ")
         stud[roll] = name
         ch = input(" Do you want to add more records(y/n): ")
     pickle.dump(stud, File)
     File.close()
         # Reading the FIle
      F = open("student.dat", "rb")
      std_rec = pickle.load(F)
      F.close()
      k = std_rec.keys()
      R = int(input("\n Enter Roll number to search: "))
      found = 'n'
         # Searching record
      for key in k :
         if key == R :
             found = 'y'
             print (" Name is ", std_rec[key])
             break
      else :
          print ("Roll does not exist in the file")
Output:
    ===================== RESTART: E:/p1.py ====================
    Enter Roll Number: 1
    Enter Name: Shilpi Gupta
      Do you want to add more records(y/n): y
    Enter Roll Number: 2
    Enter Name: Aman Kumar
      Do you want to add more records(y/n): y
    Enter Roll Number: 3
    Enter Name: Sakshi Kumari
      Do you want to add more records(y/n): n
     Enter Roll number to search: 2
     Name is Aman Kumar
Name of Experiment: Prog to write dictionary in a binary file
Q12. Write a program to create a binary file Result.dat, in which store some records.
     The records should be in dictionary form as follows,
      Key      values
      Roll Name, Marks
Program:
     import pickle
     file = open("Result.dat", "wb")
     Stud = dict ( )
     ch = 'y'
     while ch=='y' :
         roll = int (input ("Enter Roll No: "))
         name = input ("Enter Name : ")
         marks = int (input ("Enter Marks : "))
         Stud[roll] = [name, marks]            # roll is key, name & marks are values
         ch = input ("Do you want to add more records (y/n) : ")
     pickle.dump (Stud, file)
     file.close ( )
Output:
    Enter Roll No: 1
    Enter Name : Suraj Kumar
    Enter Marks : 345
    Do you want to add more records (y/n) : y
     Enter Roll No: 2
     Enter Name : Ravi Shekhar
     Enter Marks : 456
     Do you want to add more records (y/n) : y
     Enter Roll No: 3
     Enter Name : Sunidhi Verma
     Enter Marks : 387
     Do you want to add more records (y/n) : n
Name of Experiment: Prog to update records in a binary file
Q13. Write a program to read the contents of the file Result.dat, which is created in
     the above program. Now ask from the user to input any roll and update their
     marks.
Program:
      import pickle
      File = open ("Result.dat", "rb+")
      record = pickle.load(File)         # To read object from the file
      print (record)
      found = 0
      Rno = int (input ("\nEnter the roll number whose marks is to be updated: "))
      for R in record : # The keys roll will come in Rec one by one
         if R == Rno :
            print ("Current data is: ")
            print (" Roll : ", R)
            print (" Name : ", record[R][0])
            print (" Marks: ", record[R][1])
            record[R][1] = input ("Enter new Marks : ")
            found = 1
      if found == 1 :
         File.seek(0)     # move the file pointer to the begining of the file
         pickle.dump(record, File)
         print ("Marks Updated")
      else:
           print ("Record Not found")
      File.close()
      # To check the file content
      print ("\nUpdated file content is ")
      f = open ("Result.dat", "rb")
      S = pickle.load(f)
      print (S)
      f.close ( )
Output:
    ===================== RESTART: E:/p3.py ====================
    {1: ['Suraj Kumar', 345], 2: ['Ravi Shekhar', '423'], 3: ['Sunidhi Verma', 387]}
     Enter the roll number whose marks is to be updated: 2
     Current data is:
        Roll : 2
        Name : Ravi Shekhar
        Marks: 423
     Enter new Marks : 456
     Marks Updated
     Updated file content is
     {1: ['Suraj Kumar', 345], 2: ['Ravi Shekhar', '456'], 3: ['Sunidhi Verma', 387]}
Name of Experiment: Prog to create and read csv file
Q14. Write a program to ask from the user to enter details such as Roll, Name,
     Marks and store some records in a csv file stdrecord.csv.
     Now Write another program to read the above created file stdrecord.csv and
     display the file content on the screen, also count number of records.
Program:         Program to write in the csv file
Output:
                   Created csv file stdrecord.csv
         Program to read from the csv file
Output
Name of Experiment: Prog to search records in a csv file
Q15. Write a function which accepts user id as parameter and search it in a csv file
     User.csv and prints the corresponding password.
     Now write the main program to create a csv file User.csv to store some user’s
     name and password. Ask from the user to input user name and print its
     password. Call the above defined function at proper place.
Program:
   import csv
   def Search(ID):
     found='n'
     F = open("User.csv", 'r')
     FileRead = csv.reader(F)
     heading = next(FileRead)
      for row in FileRead:
           if (row[0] == ID) :
                print ("Password is: ", row[1])
                break
      else:
          print("User does not exist")
      F.close()
   # Main Program
   Fields = ['User_Id', 'password']
   ch = 'y'
   F = open("User.csv", 'w', newline='')
   Fwrite = csv.writer(F)
   Fwrite.writerow(Fields)
   while ch=='y' :
      uid = input ("Enter User Id : ")
      pwd = input ("Enter password of maximum 6 character: ")
      Row = [uid, pwd]
      Fwrite.writerow(Row)
      ch = input ("Do you want to add more (y/n): ")
   print ("File created")
   F.close()
   # Ask from the user to enter User name
   uname = input ("Enter User Name: ")
   Search(uname) # Calling function to print password
Output: