Practical 1
Practical 1
import pickle
def search(file):
flag = False
r = int(input("Enter roll no to be searched :"))
while True:
try:
rec = pickle.load(file)
if rec['Rollno'] == r:
print('Roll Num:', rec['Rollno'])
print('Name:', rec['Name'])
print('Marks:', rec['Marks'])
flag = True
except EOFError:
break
if flag == False:
print('No Records found')
#main
try :
f = open('student.dat','rb')
search(f)
except Exception as e :
print(e)
finally:
f.close()
# READ AND WRITE IN CSV FILE
import csv
def write_csv(nfw):
ch='y'
while ch=='y':
userid = input("enter userid :")
password = input("Enter password :")
nfw.writerow([userid,password])
ch = input("More entry?(y/n)")
def read_csv(newFileReader):
for row in newFileReader:
print("%10s"%row[0],"%15s"%row[1])
# main
while True:
print("1. WRITE IN CSV FILE")
print("2. READ IN CSV FILE")
print("3. EXIT")
ch = input("enter your choice :")
if ch == "1" :
try :
newFile = open('Book1.csv','w',newline = '')
nfw = csv.writer(newFile)
write_csv(nfw)
except Exception as e:
print(e)
finally :
newFile.close()
elif ch == "2" :
try :
newFile = open('Book1.csv','r')
newFileReader = csv.reader(newFile)
read_csv(newFileReader)
except Exception as e:
print(e)
finally :
newFile.close()
elif ch == "3":
break
else :
print("INVALID CHOICE")
# STACK OPERATION
s=[]
def push(a):
s.append(a)
def pop():
if(s==[]):
print("Stack Underflow")
else:
print("Element Deleted is :",s.pop())
def peek():
if s == []:
print("Empty Stack")
else:
top = s[-1]
print("Top most element is :",top)
def display():
l=len(s)
for i in range(l-1,-1,-1):
print(s[i])
def menu():
c="y"
while(c=="y"):
print("1. PUSH")
print("2. POP")
print("3. PEEK")
print("4. DISPLAY")
def employeeupdate():
e=int(input("Enter the empno to update:"))
sqll=("select * from emp where empno=%s")
rl=(e,)
value=int(input("Enter the amount to be added:"))
sql=("update emp set sal = sal+%s where empno=%s")
data=(value,e)
mycursor.execute(sql,data)
mydb.commit()
employeeupdate()