0% found this document useful (0 votes)
2 views4 pages

Practical

The document contains Python code for managing student records and book information using pickle and CSV files. It includes functionalities for writing, reading, searching, and updating records, as well as counting character cases in a text file. Additionally, it implements a stack data structure with push, pop, and display operations.

Uploaded by

Shibhi Siddarth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Practical

The document contains Python code for managing student records and book information using pickle and CSV files. It includes functionalities for writing, reading, searching, and updating records, as well as counting character cases in a text file. Additionally, it implements a stack data structure with push, pop, and display operations.

Uploaded by

Shibhi Siddarth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

import pickle

while True:
print('1.write')
print('2.read')
print('3.search')
print('4.update')
print('5.exit')
ch=int(input('enter your choice:'))
if ch==1:
file1=open('story.dat','wb')
slist=[]
stu={}
total=int(input('enter no.of student:'))
for i in range (total):
stu['Name']=input('enter name:')
stu['roll no']=int(input('enter roll no:'))
stu['marks']=int(input('enter marks:'))
slist.append(stu)
pickle.dump(slist,file1)
stu={}
file1.close()
elif ch==2:
file2=open('story.dat','rb')
stu={}
try:
while True:
stu=pickle.load(file2)
print(stu)
except EOFError:
file2.close()
elif ch==3:
file3=open('story.dat','rb')
stu=pickle.load(file3)
b=input('enter rollno to be searched:')
found=False
for i in stu:
if i['rollno']==b:
found=True
print('rollno found')
else:
print('not found')
elif ch==4:
file4=open('story.dat','rb+')
rollno=input('enter rollno to be updated:')
found=False
try:
while True:
pos=file4.tell()
stu=pickle.load(file4)
print(list(stu))
for i in stu:
if i['rollno']==int('rollno'):
i['marks']=int(input('enter new mark'))
print(i)
file4.seek(pos)
pickle.dump(stu,file4)
found=True
except EOFError:
if found==False:
print('not updated')
else:
print('updated')
file4.close()

import csv
while True:
print('1.write')
print('2.read')
print('3.search')
print('4.exit')
ch=int(input('enter choice'))
if ch==1:
f1=open('books.csv','w')
bdata=csv.writer(f1)
bdata.writerow(['bookno','bookname','author','price'])
for i in range(3):
bookno=int(input('enter no:'))
bookname=input('enter name')
author=input('enter author name')
price=int(input('enter price:'))
book=[bookno,bookname,author,price]
bdata.writerow(book)
f1.close()
elif ch==2:
f2=open('books.csv','r',newline='\r\n')
bdata=csv.reader(f2)
for i in bdata:
print(i)
f2.close()
elif ch==3:
f3=open('books.csv','r',newline='\r\n')
bdata=csv.reader(f3)
author=input('enter author:')
for i in bdata:
if i[2]==author:
print('data found')
f3.close()
elif ch==4:
break

def countcase():
file1=open('text.txt','r')
file2=open('upper.txt','a')
file3=open('lower.txt','a')
lines=file1.read()
for i in lines:
if i.isupper():
file2.write(i)
if i.islower():
file3.write(i)
file1.close()
file2.close()
file3.close()
countcase()

file2=open('upper.txt','r')
line=file2.read()
print(line)

file3=open('lower.txt','r')
line=file3.read()
print(line)'''

def isempty(stk):
if stk==[]:
return True
else:
return False
def push(stk,item):
stk.append(item)
top=len(stk)-1
def pop(stk):
if isempty(stk):
return 'underflow'
else:
item=stk.pop()
if len(stk)==0:
top=None
else:
top=len(stk)-1
return item
def display(stk):
if isempty(stk):
print('stack empty')
else:
top=len(stk)-1
print(stk[top],'-top')
for i in range(top-1,-1,-1):
print(stk(i))
stk=[]
top=None
while True:
print('1.push')
print('2.pop')
print('3.display')
print('4.exit')
ch=int(input('enter choice:'))
if ch==1:
item=int(input('enter item:'))
push(stk,item)
elif ch==2:
item=pop(stk)
if item=='underflow':
print('stack empty!')
else:
print('popped item:',item)
elif ch==3:
item=display(stk)
if item=='underflow':
print('stack empty!')
else:
print(item)
elif ch==4:
break
def count():
file=open('poem.txt','r')
line=file.read()
u=0
l=0
a=0
d=0
s=0
sym=0
for i in line:
if i.isupper():
u+=1
elif i.islower():
l+=1
if i.isalpha():
a+=1
elif i.isdigit():
d+=1
elif i.isspace():
s+=1
else:
sym+=1
print('uppercase:',u)
print('lowercase:',l)
print('alphabet:',a)
print('digit:',d)
print('space:',s)
print('symbol:',sym)
file.close()
count()

You might also like