COMPUTER SCIENCE (083)
FILE HANDLING
CSV FILE HANDLING (5 MARKS)
#1. Creation of a book detail [bid, bname, auname, price]
import csv
def create_csv():
fp=open("sample.csv","w",newline='')
obj=csv.writer(fp)
n=int(input("Enter of books:"))
for i in range(n):
bid=int(input("Enter book ID:"))
bname=input("Enter book name:")
auname=input("Enter author name:")
price=float(input("Enter the price"))
data=[bid,bname,auname,price]
obj.writerow(data)
fp.close()
#2. Display the content of CSV file
import csv
def display_csv():
fp=open("sample.csv","r")
data=csv.reader(fp)
for i in data:
print(i)
fp.close()
#3. Count the number of record in CSV
import csv
def count_csv():
fp=open("sample.csv","r")
data=csv.reader(fp)
c=0
for i in data:
c=c+1
print("Number of records=",c)
fp.close()
#4. Search in CSV - Display author name and price of "python" book [bid,bname,auname,price]
import csv
def search_csv():
fp=open("sample.csv","r")
data=csv.reader(fp)
for i in data:
if i[1]=="python":
print(i[2],i[3])
fp.close()
#5. Search in CSV - Display authorname,bname and price of all books price is more than 500.0
import csv
def search2_csv():
fp=open("sample.csv","r")
data=csv.reader(fp)
for i in data:
if float(i[3])>500.0:
print(i[1],i[2],i[3])
fp.close()
Page 1 of 9
COMPUTER SCIENCE (083)
BINARY FILE (5 MARKS)
#1. Creation a book detail [bid,bname,auname,price]
import pickle
def create_bin():
fp=open("sample.dat","wb")
n=int(input("Enter of books:"))
for i in range(n):
bid=int(input("Enter book ID:"))
bname=input("Enter book name:")
auname=input("Enter author name:")
price=float(input("Enter the price"))
data=[bid,bname,auname,price]
pickle.dump(data,fp)
fp.close()
#2. Display in Binary file
import pickle
def display_bin():
fp=open("sample.dat","rb")
try:
while True:
data=pickle.load(fp)
print(data)
except:
fp.close()
#3. Count in Binary file
import pickle
def count_bin():
fp=open("sample.dat","rb")
c=0
try:
while True:
data=pickle.load(fp)
c=c+1
except:
fp.close()
print("Number of record=",c)
#4. Search - Display author name and price of "python" book & display its count.
import pickle
def search_bin():
fp=open("sample.dat","rb")
c=0
try:
while True:
data=pickle.load(fp)
if data[1]=="python":
print(data[2],data[3])
c=c+1
except:
fp.close()
print("Total record=",c)
Page 2 of 9
COMPUTER SCIENCE (083)
#5. Search - Display author name,bname and price of all books whose price is more than 500.0
and give its count.
import pickle
def search2_bin():
fp=open("sample.dat","rb")
c=0
try:
while True:
data=pickle.load(fp)
if data[3]>500.0:
print(data[1],data[2],data[3])
c=c+1
except:
fp.close()
print("Total record=",c)
Steps in Update
1. Define the function
2. Open the file in ‘rb+’ mode
3. while True:
a. Get the position
b. Load the data
c. Check the data for condition
i. Update the new value in the data
ii. Seek/move to the position
iii. Dump the updated data
#6. Update - Increase the price of python book with 150 rs [bid,bname,auname,price]
import pickle
def update_bin():
fp=open("sample.dat","rb+")
try:
while True:
pos=fp.tell()
data=pickle.load(fp)
if data[1]=="python":
data[3]+=150
fp.seek(pos)
pickle.dump(data,fp)
except:
fp.close()
Page 3 of 9
COMPUTER SCIENCE (083)
TEXT FILE HANDLING (3 MARKS)
#CHARACTER ACCESS
#1. Count no. of 'm'
def COUNT_M():
fp=open("sample.txt","r")
data=fp.read()
c=0
for i in data:
if i=='M' or i=='m':
c=c+1
print("Total count=",c)
fp.close()
#2. Count no. of 'q' & 's'
def COUNT_QS():
fp=open("sample.txt","r")
data=fp.read()
qc=sc=0
for i in data:
if i=='Q' or i=='q':
qc=qc+1
if i=='S' or i=='s':
sc=sc+1
print("Total Q count=",qc)
print("Total S count=",sc)
fp.close()
#3. Count no. of vowels
def COUNT_VOWELS():
fp=open("sample.txt","r")
data=fp.read()
v=0
for i in data:
if i in 'AEIOUaeiou':
v+=1
print("Total vowels=",v)
fp.close()
#4. Count no. of digits
def COUNT_DIGITS():
fp=open("sample.txt","r")
data=fp.read()
d=0
for i in data:
if i.isdigit():
d+=1
print("Total digits=",d) fp.close()
#5. Count no. of space
def COUNT_SPACE():
fp=open("sample.txt","r")
data=fp.read()
d=0
for i in data:
if i.isspace():
d+=1
print("Total space=",d) fp.close()
Page 4 of 9
COMPUTER SCIENCE (083)
#WORD ACCESS
#1. Count no. of 'our'
def COUNT_OUR():
fp=open("sample.txt","r")
data=fp.read().lower()
word=data.split()
c=0
for i in word:
if i=='our':
c=c+1
print("Total count=",c)
fp.close()
#2. Count no. of 'world' & 'python' individually
def COUNT_WORDS():
fp=open("sample.txt","r")
data=fp.read().lower()
word=data.split()
c1=0
c2=0
for i in word:
if i =='world':
c1=c1+1
if i =='python':
c2=c2+1
print("Total count of world=",c1)
print("Total count of python=",c2)
fp.close()
#3. Count no. of word whose length is more than 5 & print the words
def COUNT_WORD5():
fp=open("sample.txt","r")
data=fp.read()
word=data.split()
c=0
for i in word:
if len(i)>5:
print(i)
c=c+1
print("Total count=",c)
fp.close()
#4. Count no. of word starts with 'w' & print those words
def COUNT_STARTW():
fp=open("sample.txt","r")
data=fp.read()
word=data.split()
c=0
for i in word:
if i[0]=='W' or i[0]=='w':
print(i)
c=c+1
print("Total count=",c)
fp.close()
Page 5 of 9
COMPUTER SCIENCE (083)
#5. Count no. of word ends with 'r' & print those words
def COUNT_ENDR():
fp=open("sample.txt","r")
data=fp.read()
word=data.split()
c=0
for i in word:
if i[-1]=='R' or i[-1]=='r':
print(i)
c=c+1
print("Total count=",c)
fp.close()
#6. Count no. of word starts with vowels
def COUNT_STARTVOWELS():
fp=open("sample.txt","r")
data=fp.read()
word=data.split()
c=0
for i in word:
if i[0] in 'AEIOUaeiou':
print(i)
c=c+1
print("Total count=",c)
fp.close()
#7. Count no. of word contain 'e'
def COUNT_CONTAIN_E():
fp=open("sample.txt","r")
data=fp.read()
word=data.split()
c=0
for i in word:
if 'e' in i or 'E' in i:
print(i)
c=c+1
print("Total count=",c)
fp.close()
#8. Count no. of word contain vowels
def COUNT_CONTAIN_VOWELS(): def COUNT_CONTAIN_VOWELS():
fp=open("sample.txt","r") fp=open("sample.txt","r")
data=fp.read() data=fp.read()
word=data.split() word=data.split()
c=0 c=0
for i in word: for i in word:
v=0 for j in i:
for j in i: if j in 'AEIOUaeiou':
if j in 'AEIOUaeiou': c+=1
v+=1 print(i)
if v!=0: break
print(i) print("Total count=",c)
c=c+1 fp.close()
print("Total count=",c)
fp.close()
Page 6 of 9
COMPUTER SCIENCE (083)
#9. Count no. of word contain consonants (non vowels)
def COUNT_CONTAIN_NOVOWELS():
fp=open("sample.txt","r")
data=fp.read()
word=data.split()
c=0
for i in word:
v=0
for j in i:
if j in 'AEIOUaeiou':
v+=1
break
if v==0:
print(i)
c=c+1
print("Total count=",c)
fp.close()
#10.Print the words starting with 'F' in reverse
def PRINTREV_STARTF():
fp=open("sample.txt","r")
data=fp.read()
word=data.split()
c=0
for i in word:
if i[0]=='F' or i[0]=='f':
print(i[::-1],end=' ')
else:
print(i,end=' ')
fp.close()
#11.Print the biggest word and its length. Call the function.
def BIG_WORD():
fp=open("sample.txt","r")
data=fp.read()
word=data.split()
bigword=''
big=0
for i in word:
if len(i)>big:
bigword=i
big=len(i)
print(bigword,len(bigword))
fp.close()
BIG_WORD()
Page 7 of 9
COMPUTER SCIENCE (083)
#LINE ACCESS
#1. Count no. of line starts with 'w' & print those lines
def COUNT_LINE_STARTW():
fp=open("sample.txt","r")
data=fp.read()
line=data.splitlines()
c=0
for i in line:
if i[0]=='W' or i[0]=='w':
print(i)
c=c+1
print("Total count=",c)
fp.close()
#2. Count no. of line ends with 'g' & print those lines
def COUNT_LINE_ENDG():
fp=open("sample.txt","r")
data=fp.read()
line=data.splitlines()
c=0
for i in line:
if i[-1]=='G' or i[-1]=='g':
print(i)
c=c+1
print("Total count=",c)
fp.close()
#3. Display biggest line and its length
def DISPLAY_BIG_LINE():
fp=open("sample.txt","r")
data=fp.read()
line=data.splitlines()
bigline=''
big=0
for i in line:
if len(i)>big:
bigline=i
big=len(i)
print(bigline,len(bigline))
fp.close()
#4. Count no. of line starts with vowels
def COUNT_STARTVOWELS():
fp=open("sample.txt","r")
data=fp.read()
line=data.splitlines()
c=0
for i in line:
if i[0] in 'AEIOUaeiou':
print(i)
c=c+1
print("Total count=",c)
fp.close()
Page 8 of 9
COMPUTER SCIENCE (083)
#5. Display the line starts with articles (an, a, the)
def LINE_ARTICLES():
fp=open("sample.txt","r")
data=fp.read()
line=data.splitlines()
for i in line:
word=i.split()
if word[0].lower()in ['a','an','the']:
print(i)
fp.close()
#6. Display Count no. of line starts and ends with the same letter
def LINE_SAMELETTER():
fp=open("sample.txt","r")
data=fp.read()
line=data.splitlines()
c=0
for i in line:
if i[0]==i[-1]:
print(i)
c+=1
print("Total count=",c)
fp.close()
#7. To copy line starts with "#" in another file named "output.txt"
def LINE_COPY():
fp1=open("sample.txt","r")
fp2=open("output.txt","w")
data=fp1.read()
line=data.splitlines()
for i in line:
if i[0]=='#':
fp2.write(i)
fp2.write('\n')
print("Lines copied")
fp1.close()
fp2.close()
#8. Print the reverse of the line
def REVERSE_LINE():
fp=open("sample.txt","r")
data=fp.read()
line=data.splitlines()
for i in line:
print(i[::-1])
fp.close()
#9. Display the line that do not contain 'fi'
def DISPLAY_LINE():
fp=open("sample.txt","r")
data=fp.read()
line=data.splitlines()
for i in line:
if 'fi' not in i.lower():
print(i)
fp.close()
Page 9 of 9