Binary Files with (class, lists, Dictionary )
Class customer:Def getdata(self):
# get i/p
Def dispdata(self):
# print O/p
1. Write a metheod to add customer details in CUST.dat using the class customer, given in the class note book
def addfn():
f1=open("cust.dat","ab")
c1=customer()
c1.getdata()
pickle.dump(c1,f1)
print "added"
f1.close() / 1. A list contains list of cno,cname,caddress, cphone. Write a method to add customer details in to cust.dat
def addfn():
f1=open("cust.dat","ab")
L=[]
Cno=input("Enter cno")
cnameraw_input("Enter cname")
caddress=raw_input("Enter c address")
cphone=input("Enter C Phone")
L=[cno,cname,caddress,cphone]
pickle.dump(L,f1)
print "added"
f1.close() / 2. A dictionary contains list of cno,cname,caddress, cphone. Write a method to add customer details in to cust.dat
def addfn():
f1=open("cust.dat","ab")
d={}
d["cno"]=input("Enter cno")
d["cname"]=raw_input("Enter cname")
d["caddress"]=raw_input("Enter c address")
d["cphone"]=input("Enter C Phone")
pickle.dump(d,f1)
print "added"
f1.close()
3. Write a metheod to search customer details from cust.dat, when customer name=”elden”using the class customer
def searchfn():
f1=open("cust.dat","rb")
c1=customer()
try:
While True:
c1=pickle.load(f1)
if(c1.cname=="Elden"):
c1.displaydata()
except EOFError:
pass
f1.close() / 4. A list contains list of cno,cname,caddress, cphone. Write a method to search customer details from cust.dat, when customer name=”elden”
def searchfn():
f1=open("cust.dat","rb")
L=[]
try:
While True:
L=pickle.load(f1)
if(L[1]=="Elden"):
print L[0],L[1],L[2],L[3]
except EOFError:
pass
f1.close() / 5. A dictionary contains list of cno,cname,caddress, cphone. Write a method to search customer details from cust.dat, when customer name=”elden”
def searchfn():
f1=open("cust.dat","rb")
d={}
try:
While True:
d=pickle.load(f1)
if(d["cname"]=="Elden"):
print d["cno"],d["cname"],d["caddress"],d["cphone"]
except EOFError:
pass
f1.close()
6. Write a metheod to delete customer details from cust.dat, when customer phone no is 123456 using the class customer
def deletefn():
f1=open("cust.dat","rb")
f2=open("temp.dat","ab")
f2.truncate()
c1=customer()
try:
While True:
c1=pickle.load(f1)
if(c1.cphone==123456):
pickle.dump(c1,f2)
except EOFError:
pass
f1.close()
f2.close()
os.remove("cust.dat")
os.rename("temp.dat","cust.dat")
print "Deleted" / 7. A list contains list of cno,cname,caddress, cphone. Write a metheod to delete customer details from cust.dat, when customer phone no is 123456
def deletefn():
f1=open("cust.dat","rb")
f2=open("temp.dat","ab")
f2.truncate()
L=[]
try:
While True:
L=pickle.load(f1)
if(L[3]==123456):
pickle.dump(L,f2)
except EOFError:
pass
f1.close()
f2.close()
os.remove("cust.dat")
os.rename("temp.dat","cust.dat")
print "Deleted" / 8. A dictionary contains list of cno,cname,caddress, cphone. Write a metheod to delete customer details from cust.dat, when customer phone no is 123456
def deletefn():
f1=open("cust.dat","rb")
f2=open("temp.dat","ab")
f2.truncate()
d={}
try:
While True:
d=pickle.load(f1)
if(d["cphone"]==123456):
pickle.dump(d,f2)
except EOFError:
pass
f1.close()
f2.close()
os.remove("cust.dat")
os.rename("temp.dat","cust.dat")
print "Deleted"
9. Write a metheod to update customer details from cust.dat, when customer phone no is 123456 using the class customer
def updateefn():
f1=open("cust.dat","rb")
f2=open("temp.dat","ab")
f2.truncate()
c1=customer()
try:
While True:
c1=pickle.load(f1)
if(c1.cphone==123456):
pickle.dump(c1,f2)
else:
c1.getdata()
pickle.dump(c1,f2)
except EOFError:
pass
f1.close()
f2.close()
os.remove("cust.dat")
os.rename("temp.dat","cust.dat")
print "Updated" / 10. A list contains list of cno,cname,caddress, cphone. Write a metheod to update customer details from cust.dat, when customer phone no is 123456
def updateefn():
f1=open("cust.dat","rb")
f2=open("temp.dat","ab")
f2.truncate()
L=[]
try:
While True:
L=pickle.load(f1)
if(L[3]==123456):
pickle.dump(L,f2)
else:
Cno=input("Enter cno")
cnameraw_input("Enter cname")
caddress=raw_input("Enter c add")
cphone=input("Enter C Phone")
L=[cno,cname,caddress,cphone]
pickle.dump(L,f2)
except EOFError:
pass
f1.close()
f2.close()
os.remove("cust.dat")
os.rename("temp.dat","cust.dat")
print "Updated"
/ 11. A dictionary contains list of cno,cname,caddress, cphone. Write a metheod to update customer details from cust.dat, when customer phone no is 123456
def updateefn():
f1=open("cust.dat","rb")
f2=open("temp.dat","ab")
f2.truncate()
d={}
try:
While True:
d=pickle.load(f1)
if(d["cphone"]==123456):
pickle.dump(d,f2)
else:
d["cno"]=input("Enter cno")
d["cname"]=raw_input("Enter cname")
d["caddress"]=raw_input("Enter c address")
d["cphone"]=input("Enter C Phone")
pickle.dump(d,f2)
except EOFError:
pass
f1.close()
f2.close()
os.remove("cust.dat")
os.rename("temp.dat","cust.dat")
print "Updated"
12. Write a metheod to copy customer details from cust.dat in to custcopy.dat , when customer phone no is 123456 using the class customer
ef copyfn():
f1=open("cust.dat","rb")
f2=open("custcopy.dat","ab")
f2.truncate()
c1=customer()
try:
While True:
c1=pickle.load(f1)
if(c1.cphone!=123456):
pickle.dump(c1,f2)
except EOFError:
pass
f1.close()
f2.close()
print "Copied" / 13. A list contains list of cno,cname,caddress, cphone. Write a metheod to copy customer details from cust.dat in to custcopy.dat, when customer phone no is 123456
def copyfn():
f1=open("cust.dat","rb")
f2=open("custcopy.dat","ab")
f2.truncate()
L=[]
try:
While True:
L=pickle.load(f1)
if(L[3]!=123456):
pickle.dump(L,f2)
except EOFError:
pass
f1.close()
f2.close()
print "Copied" / 14. A dictionary contains list of cno,cname,caddress, cphone. Write a metheod to copy customer details from cust.dat into custcopy.dat, when customer phone no is 123456
def copyfn():
f1=open("cust.dat","rb")
f2=open("custcopy.dat","ab")
f2.truncate()
d={}
try:
While True:
d=pickle.load(f1)
if(d["cphone"]!=123456):
pickle.dump(d,f2)
except EOFError:
pass
f1.close()
f2.close()
print "Copied"
class stack:
def __init__(self):
self.s=[]
self.top=-1
def push(self):
self.top=self.top+1
eno=input("Enter Employee No:")
ename=raw_input("Enter Employee Name:")
esalary=input("Enter Employee Salary:")
a=[eno,ename,esalary]
self.s.append(a)
print "Now Top.....", self.top
print "Employee Details Added in to Satck"
def pop(self):
if(self.s==[]):
print "Stack is Empty"
else:
self.top=self.top-1
a=self.s.pop()
print "Now top...... ", self.top
print "Deleted Employee Detail is =" , a
def display(self):
print "top...... ", self.top
for i in range(len(self.s)-1,-1,-1):
print self.s[i]
def cls(self):
print "\n" * 50
def main():
ss=stack()
ss.cls()
ans="y"
while(ans=="y"):
print "EMPLOYEE DETAILS (STACK)"
print "1. Push"
print "2. Pop"
print "3. Display"
choice=input("Enter ur choice:")
if(choice==1): / ss.push()
elif(choice==2):
ss.pop()
elif(choice==3):
ss.display()
ans=raw_input("Do u want to continue(y/n):")
main()
------class queue:
def __init__(self):
self.q=[]
self.front=-1
self.rear=-1
def enqueue(self):
self.rear=self.rear+1
eno=input("Enter Employee No:")
ename=raw_input("Enter Employee Name:")
esalary=input("Enter Employee Salary:")
a=[eno,ename,esalary]
self.q.append(a)
print "Now Top.....", self.rear
print "Employee Details Added in to Queue "
def dequeue(self):
if(self.q==[]):
print "Queue is Empty"
else:
self.front=self.front+1
a=self.q.pop(0)
print "Now front...... ", self.front
print "Deleted Employee Detail is =" , a
def display(self):
print "top...... ", self.top
for i in range(0,len(self.q),1):
print self.q[i]
def cls(self):
print "\n" * 50 / def main():
qq=queue()
qq.cls()
ans="y"
while(ans=="y"):
print "EMPLOYEE DETAILS (Queue)"
print "1. Enqueue"
print "2. Dequeue"
print "3. Display"
choice=input("Enter ur choice:")
if(choice==1):
qq.enqueue()
elif(choice==2):
qq.dequeue()
elif(choice==3):
qq.display()
ans=raw_input("Do u want to continue(y/n):")
main()
Define a function stackadd(s,top): to add the following Employee datas in to stack and show the status of top
Eno,ename,salary
def stackadd(s,top):
top=top+1
eno=input("Enter Employee No:")
ename=raw_input("Enter Employee Name:")
esalary=input("Enter Employee Salary:")
a=[eno,ename,esalary]
s.append(a)
print "Now Top.....", self.top
print "Employee Details Added in to Satck"
for i in range(len(s)-1,-1,-1):
print s[i] / Define a function queueadd(q,front,rear): to add the following Employee datas in to queue and show the status
Eno,ename,salary
def queueadd(q,front,rear):
rear= rear+1
eno=input("Enter Employee No:")
ename=raw_input("Enter Employee Name:")
esalary=input("Enter Employee Salary:")
a=[eno,ename,esalary]
q.append(a)
print "Now Top.....", rear
print "Employee Details Added in to Queue "
for i in range(0,len(q),1):
print q[i]
Define a function stackdelete(s,top): to delete the following Employee datas from stack and show the status of top
Eno,ename,salary
def stackdelete(s,top):
if(s==[]):
print "Stack is Empty"
else:
top= top-1
a= s.pop()
print "Now top...... ", top
print "Deleted Employee Detail is =" , a
for i in range(len(s)-1,-1,-1):
print s[i] / Define a function queueadd(q,front,rear): to delete the following Employee datas from the queue and show the status
Eno,ename,salary
def queueadd(q,front,rear):
if(q==[]):
print "Queue is Empty"
else:
front=front+1
a=q.pop(0)
print "Now front...... ",front
print "Deleted Employee Detail is =" , a
for i in range(0,len(q),1):
print q[i]
Linear Search:
def lsearch(self,a,n):
for i in range(0,len(a),1):
if(a[i]==n):
pos=i
break
return(pos)
def main():
a=list(input("ENTER LIST OF PRODUCT PRICES IN ASCENDING ORDER"))
n=input("ENTER THE PRICE SEARCH THE LOCATION")
pos= lsearch(a,n)
print "pos=",pos
main() / Binary Search: PROGRAM TO SEARCH THE LOCATION GIVEN EMPLOYEE NAME USING BINARY SEARCH
def bsearch(self,a,n):
f=0
l=len(a)-1
while(f<=l):
mid=int((f+l)/2)
if(a[mid]>n):
l=mid-1
if(a[mid]<n):
f=mid+1
if(a[mid]==n):
pos=mid
break
return(pos) / def main():
a=list(input("ENTER LIST OF Employee Names ……………………………………….in Alphebetical Order "))
n=raw_input("Enter Employee Name to search t….. …………………………………………he location.")
pos= bsearch(a,n)
print "pos=",pos
main()
Bubble sort:
PROGRAM TO SORT THE LIST VALUES IN ASCEDING ORDER OR ALPHABETICAL ORDER USING BUBBLE SORT
def bubblesort(a):
for i in range(0,len(a),1):
for j in range(1,len(a)-i,1):
if(a[j]<a[j-1]):
temp=a[j]
a[j]=a[j-1]
a[j-1]=temp
def main():
cls()
a=list(input("ENTER LIST OF VALUES TO BE …………………………………SORTED"))
bubblesort(a)
print a
main() / Selection sort: PROGRAM TO SORT THE TRAVEL LIST DETAILS(TNO,TPLACE,TCOST) BY ASCENDING ORDR OF TCOST USING SELECTION SORT
def selectionsort(self,a):
for i in range(0,len(a),1):
small=i
for j in range(i+1,len(a),1):
if(a[j][2]<a[small][2]):
small=j
temp=a[i]
a[i]=a[small]
a[small]=temp
def main():
a=list(input("ENTER LIST OF tno, tplace …………….tcost VALUES TO BE SORTED"))
ss.selectionsort(a)
print "Sorted values by T cost=",a
main() / Insertion sort: PROGRAM TO SORT THE LIST STUDENTS NAMES IN ALPHABETICAL ORDER USING INSERTION SORT
def insertionsort(a):
for i in range(1,len(a),1):
temp=a[i]
j=i-1
while((j>=0)and(a[j]>temp)):
a[j+1]=a[j]
j=j-1
a[j+1]=temp
def main():
a=list(input("ENTER LIST student name to be ……………………………………..SORTED"))
insertionsort(a)
print "Alphebetical order Sorted Names are=",a
main()
TXT Programs:
1. python program to print only alphabets from file (STORY.txt)f1=open("story.text","r")
for a in f1.read():
if a.isalpha():
print a,
f1.close() / 2. python program to count only alphabets from file (STORY.txt)
f1=open("story.text","r")
c=0
for a in f1.read():
if a.isalpha():
c=c+1
print "No of Alphabets=",c
f1.close() / 3. python program to copy only alphabets from file (STORY.txt) into strycopy.txt
f1=open("story.text","r")
f2=open("storycopy.txt","a")
for a in f1.read():
if a.isalpha():
f2.wrie(a)
f1.close()
f2.close()
4. program to print the line which is starting with the letter A or ending with letterD from the File(BooK.TXT )
f1=open("book.txt","r")
for a in f1.readlines():
if ((a[0]=='a') or(a[len(a)-2]=='d')):
print a
f1.close() / 5. program to count the line which is starting with the letter A or ending with letterD from the File(BooK.TXT )
f1=open("book.txt","r")
c=0
for a in f1.readlines():
if ((a[0]=='a') or(a[len(a)-2]=='d')):
c=c+1
print "Count=",c
f1.close() / 6. program to copy the line which is starting with the letter A or ending with letterD from the File(BooK.TXT ) to Bookcopy.txt
f1=open("book.txt","r")
f2=open("bookcopy.txt","a")
for a in f1.readlines():
if ((a[0]=='a') or(a[len(a)-2]=='d')):
f2.write(a)
f1.close()
f2.close()
7. Write a function countdo(): in python to count the number of times the word “do” and “does” present in “BOOK.txt”
def countdo():
f1=open("book.txt","r")
c1=0
c2=0
for a in f1.readlines():
c1=c1+a.count(“do”)
c2=c2+a.count(“does”)
print “Count of do=”, c1
print “Count of does=”,c2
f1.close() / 8. Write a program in python to capitalize each word from every line from Book.txt
f1=open("book.txt","r")
for a in f1.readlines():
x=a.split(" ")
for b input x:
print b.capitalize()," ",
print "\n"
f1.close() / 9.