Text Files
Text Files
No 27
def countINDIA():
f=open('myüle.txt')
s=f.read()
l=s.split()
k=0
for i in l:
if i.upper()=='INDIA':
k=k+1
print('Frequency of India is ',k)
countINDIA()
def countVowel():
ctr=0
f=open('d:\\myüle.txt')
data=f.read()
for ch in data:
if ch.lower() in 'aeiou':
ctr=ctr+1
print('Total number of vowels are :', ctr)
countVowel()
def displaywords():
f = open('data.txt','r')
s = f.read()
lst = s.split()
for x in lst:
if len(x)>3:
print(x, end=" ")
f.close()
displaywords()
OR
b)
A pre-existing text üle info.txt has some text written in it.
Write a python
function countvowel() that reads the contents of the üle
and counts the
occurrence of vowels(A,E,I,O,U) in the üle.
def countvowels():
f = open('info.txt', 'r')
s = f.read()
count = 0
for x in s:
if x in 'AEIOU':
count+=1
print(count)
f.close()
countvowels()'''
def COUNT_AND():
count=0
üle=open('STORY.TXT','r')
line = üle.read()
word = line.split()
for w in word:
if w in ['AND','and','And']:
count=count+1
üle.close()
print(count)
COUNT_AND()
OR
b)
Write a function DISPLAYWORDS( ) in python to display
the count of words starting with <t= or <T=
in a text üle 'STORY.TXT'.
def DISPLAYWORDS():
count=0
üle=open('STORY.TXT','r')
line = üle.read()
word = line.split()
for w in word:
if w[0]=="T" or w[0]=="t":
count=count+1
üle.close()
print(count)
DISPLAYWORDS()
def rdlines():
üle = open('visitors.txt','r')
for line in üle:
if line[0] == 'P':
print(line)
üle.close()
# Call the rdlines function.
rdlines()
OR
b)
Write a method in Python to read lines from a text üle
'book.txt', to ünd and
display the occurrence or the word 'are'. For example, if
the content of the
is:
Books are referred to as a man's best friend. They are very
beneücial for
mankind and have helped it evolve. Books leave a deep
impact on us and are
responsible for uplifting our mood.
|The output should be 3.
def count_word():
üle = open('book.txt','r')
count = 0
for line in üle:
words = line.split()
for word in words:
if word == 'India':
count += 1
print(count)
üle.close()
# call the function count_word().
count_word()
#5 a)
Write a method/function DISPLAYWORDS() in python to
read lines from a text üle
STORY.TXT, and display those words, which are less than 4
characters.
def displaywords():
f = open("story.txt","r")
d = f.read()
m = d.split()
for i in m:
if (len (i) <4):
print (i)
displaywords ()
OR
b)
Write a function RevText() to read a text üle "Story.txt" and
Print only word starting
with 'I' in reverse order.
Example:
If value in text üle is:
INDIA IS MY COUNTRY
Output will be: AIDNI SI MY COUNTRY.
def revtext():
f=open("story.txt","r")
s=""
while True:
d=f.readline()
if not d:
break
else:
m=d.split()
for i in m:
if i[0]=='i' or i[0]=='I':
s=s+" "+(i[::-1])
else:
s=s+" "+i
prinit(s)
s=""
revtext()
#6 a)
Write a method COUNTLINES() in Python to read lines
from text üle
8TESTFILE.TXT9 and display the lines which are starting
with any article (a,
an, the) insensitive of the case.
Example:
If the üle content is as follows:
Give what you want to get.
We all pray for everyone9s safety.
A marked diûerence will come in our country.
The Prime Minister is doing amazing things.
The COUNTLINES() function should display the output as:
The number of lines starting with any article are : 2
def COUNTLINES() :
üle = open ('TESTFILE.TXT', 'r')
List = üle.readlines()
count=0
for line in List:
words=line.split()
if words[0].lower() in (8a9,8an,8the9):
count = count + 1
print("The number of lines starting with any article are
:", count)
üle.close()
b)
Write a function GPCount() in Python, which should read
each character
of a text üle <STORY.TXT= and then count and display the
count of
occurrenceof alphabets G and P individually (including
small cases g and
p too).
Example:
If the üle content is as follows:
God helps those who help themselves.
Great way to be happy is to remain positive.
Punctuality is a great virtue.
The GPCount() function should display the output as:
The number of G or g: 3
The number of P or p : 6
def GPCount() :
üle = open ('STORY.TXT', 'r')
content = üle.read()
for i in content:
if i.lower()== 'g':
countG = countG + 1
if i.lower()== 'p':
countP = countP + 1
print ("The number of G or g : ", countG)
print ("The number of P or p : ", countP)
üle.close()
#7. a)
Suppose contentof'Myüle.txt'is
Humpty Dumpty sat on a wall
Humpty Dumpty had a great fall
All the king's horses and all the king's men
Couldn't put Humpty together again
Write a python function named RECORD to calculate total
number of characters in 8Myüle.txt9
def RECORD():
f1=open('Myüle.txt','r')
str1=f1.read()
print(len(str1))
RECORD()
OR
def CLRECORD():
f1=open('Myüle.txt','r')
l1=f1.readlines()
print(len(l1))
CLRECORD()
#8.
Write a method count_words_e()in Python to read the
content of a textüle and count
the number of words ending with 'e' in the üle.
Example: If the üle content is as follows:
An apple a day keeps the doctor away.
We all pray for everyone9s safety.
A marked diûerence will come in our country.
The count_words_e() function should display the output as:
No.of such words: 4
def count_words_e():
f1=open("MyFile.txt")
data=f1.read()
w=data.split()
c=0
for i in w:
if i[-1]=='e':
c+=1
print(c)
count_words_e()
OR
fname = "python.txt"
k= 0
f= open(fname, 'r')
words = f.read().split()
for a in words:
if (a.lower() == <to= or a.lower() == <the= ):
k=k+1
print("Number of words:", k)
f.close()
OR
b)
A text üle <PYTHON.TXT= contains alphanumeric text.
Write a program that reads this text üle and writes to
another üle <PYTHON1.TXT= entire üle except the numbers
or digits in the üle.
fh=open(<python.txt","r")
fw=open(<python1.txt","w")
rec=fh.read()
for a in rec:
if (a.isdigit() != True):
print(a,end=' ')
fw.write(a)
fh.close()
fw.close()
10 a)
Write a method/function COUNT_BLANK_SPACES() in
Python to read lines from a text üle
STORY.TXT, and display the count of blank spaces in the
text üle.
def COUNT_BLANK_SPACES():
f=open("STORY.txt",'r')
str=f.read()
x=str.split()
count=0
for i in x:
count+=1
print("Total no. blank spaces are ",count-1)
f.close()
COUNT_BLANK_SPACES():
OR
b)
Write a method/function DISPLAYWORDS() in python to
read lines from a text üle POEM.TXT, and
display those words, which are less than 4 characters.
def DISPLAYWORDS():
üle=open(>POEM.txt','r')
line = üle.read()
word = line.split()
for w in word:
if len(w)<4:
print( w)
üle.close()
DISPLAYWORDS()
11 a)
Write a function in python to count the number of lines in a
text üle 8Country.txt9 which are starting with an alphabet
8W9 or 8H9.
For example, If the üle contents are as follows:
Whose woods these are I think I know.
His house is in the village though;
He will not see me stopping here
To watch his woods üll up with snow.
The output of the function should be:
W or w : 1
H or h : 2
def count_W_H():
f = open (<Country.txt=, <r=)
W,H = 0,0
r = f.read()
for x in r:
if x[0] == <W= or x[0] == <w=:
W=W+1
elif x[0] == <H= or x[0] == <h=:
H=H+1
f.close()
print (<W or w :=, W)
print (<H or h :=, H)
OR
def countwords():
s = open("Quotes.txt","r")
f = s.read()
z = f.split ()
count = 0
for I in z:
count = count + 1
print ("Total number of words:", count)
#12 a)
Write a function COUNTLINES( ) which reads a text üle
STORY.TXT and then count and
display the number of the lines which starts and ends with
same letter irrespective of its
case . For example if the content of the text üle STORY.TXT
is :
The person has a sent a lovely tweet
Boy standing at station is very disturbed
Even when there is no light we can see
How lovely is the situation
The expected output is :
The Number of lines starting with same letter is 2
Ans :
def COUNTLINES():
ün = open("Story.txt", 'r')
lines = ün.readlines()
count = 0
for line in lines:
if line[0].lower() == line[-1].lower():
count+=1
print("The Number of lines starting with same letter
is",count)
ün.close()
OR
Ans :
def VOWEL_WORDS():
ün = open("TESTFILE.TXT" , 'r')
content = ün.read()
words = content.split()
acount,ucount = 0,0
for word in words:
if word[0] in "aA":
acount+=1
if word[0] in "uU":
ucount+=1
print("The Number of words starting with letter 8a9 is
:",acount)
print("The Number of words starting with letter 8u9 is
:",ucount)
VOWEL_WORDS()
#13 a)
Deüne a function SHOWWORD () in python to read lines
from a text üle STORY.TXT, and
display those words, whose length is less than 5.
def SHOWWORD () :
c=0
üle=open(8STORY.TXT,'r')
line = üle.read()
word = line.split()
for w in word:
if len(w)<5:
print( w)
üle.close()
OR
def count H( ):
f = open (<para.txt= , <r= )
lines =0
L=f. readlines ()
for i in L:
if i [0]== 8H9:
lines +=1
print (<No. of lines are: < , lines)
#14.
W rite a method SHOWLINES() in Python to read lines
from text üle
8TESTFILE.TXT9 and display the lines which do not contain
'ke'.
Example: If the üle content is as follows:
An apple a day keeps the doctor away.
We all pray for everyone9s safety.
A marked diûerence will come in our country.
The SHOWLINES() function should display the output as:
We all pray for everyone9s safety.
def SHOWLINES():
f=open("testüle.txt")
for line in f:
if 'ke' not in line:
print(line.strip())
f.close()
OR
def RainCount():
f=open('rain.txt')
data=f.read()
data=data.upper()
data=data.split()
c=data.count('RAIN')
print('Rain -',c)