Prac 11 15
Prac 11 15
Prac 11 15
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()
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
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
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]}
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.
Output:
Created csv file stdrecord.csv
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)
# 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()