Files Board Questions
Files Board Questions
FILES
(A)Write a Python function that displays all the words containing @cmail
from a text file "Emails.txt".
def find():
f=open("Emails.txt")
L=f.read()
LIST=L.split()
for i in LIST:
if i.find('@cmail')!=-1:
print(i)
find()
(B)Write a Python function that finds and displays all the words longer than
5 characters from a text file "Words.txt".
def find():
f=open("Words.txt")
L=f.read()
LIST=L.split()
for i in LIST:
if len(i)>=5:
print(i)
find()
CSV
import csv# the writer object converts the user's data into a delimeted
string.
#the writer function returns a writer object
# to avoid blank lines use new line argument
def create():
f=open("happiness.csv",'w',newline='')
wr=csv.writer(f)
wr.writerow(['name','population','sample size','happy'])
while True:
name=input("enter name...")
population=int(input("enter population..."))
size=int(input("enter size.."))
happy=int(input("enter the number of people happy.."))
data=[ name, population, size, happy]
wr.writerow(data)
ch=input("do you want to continue")
if ch in "Nn":
break
f.close()
#create()
def read():
f=open("happiness.csv",'r')
read=csv.reader(f)
for i in read:
print(i)
read()
print("searching...")
def search():
f=open("happiness.csv",'r')
read=csv.reader(f)
next(read)
for i in read:
if(int(i[1])>300000):
print(i)
search()
CSV
import csv
def create():
f=open("candidate_data.csv",'w',newline='')
wr=csv.writer(f)
wr.writerow(['candidate_id','candidate_name','designation','experience'])
while True:
candidate_id=input("enter candidateid...")
candidate_name=input("enter candidate_name...")
designation=input("enter designation..")
exp=int(input("enter the experience.."))
data=[ candidate_id, candidate_name, designation, exp]
wr.writerow(data)
ch=input("do you want to continue")
if ch in "Nn":
break
f.close()
create()
def read():
f=open("candidate_data.csv",'r')
read=csv.reader(f)
for i in read:
print(i)
read()
print("searching...")
def search():
f=open("candidate_data.csv",'r')
read=csv.reader(f)
next(read)
for i in read:
if(int(i[3])>10):
print(i)
search()
BINARY FILES
import csv
def create():
f=open("candidate_data.csv",'w',newline='')
wr=csv.writer(f)
wr.writerow(['candidate_id','candidate_name','designation','experience'])
while True:
candidate_id=input("enter candidateid...")
candidate_name=input("enter candidate_name...")
designation=input("enter designation..")
exp=int(input("enter the experience.."))
data=[ candidate_id, candidate_name, designation, exp]
wr.writerow(data)
ch=input("do you want to continue")
if ch in "Nn":
break
f.close()
create()
def read():
f=open("candidate_data.csv",'r')
read=csv.reader(f)
for i in read:
print(i)
read()
print("searching...")
def search():
f=open("candidate_data.csv",'r')
read=csv.reader(f)
next(read)
for i in read:
if(int(i[3])>10):
print(i)
search()
EXTRA QUESTIONS
BINARY FILES
import pickle
def write():
f=open("CINEMA.dat","wb")
while True:
DIC={}
MNO=input("Enter the MOVIE NUMBER :")
MNAME=input("Enter MOVIE NAME:")
MTYPE=input("Enter the MTYPE")
DIC[MNO]=[MNAME,MTYPE]
pickle.dump(DIC,f)
ch=input("do you want to continue (y/n)")
if ch=='n':
break
#write()
def findType(mtype):
f=open("CINEMA.dat","rb")
try:
while(True):
s=pickle.load(f)
for i,j in s.items():
value=j
if(value[1]==mtype):
print(s)
except EOFError:
print("end of file reached")
findType('kids')
BINARY FILES
import pickle
def copydata():
f=open("company.dat",'rb')
f1=open("edutechs.dat",'wb')
try:
while(True):
data=pickle.load(f)
if(data[1]=='edutech')
pickle.dump(data,f1)
except EOFError:
f.close()
f1.close()
print("end of file reached....")
return c
print(copydata())
QUESTION 32:
import csv
def accept()
f=open("participants.csv",'w',newline='')
wr=csv.writer(f)
wr.writerow(['ID','NAME','EVENT'])
Id=int(input("enter id..."))
name=input("enter name...")
event=input("enter event name...")
data=[Id,name,event]
wr.writerow(data)
def eventcount():
f=open("participants.csv",'r')
read=csv.reader(f)
c=0
for i in read:
if(i[2]=='Robocon'):
c+=1
print(c)