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

Python Text File Analysis Functions

The document contains a series of Python programming tasks focused on reading from a text file named 'story.txt'. It includes functions to count letters, words, vowels, and specific lines, as well as to manipulate and create new files based on the content of 'story.txt'. Each task is presented with code snippets demonstrating the required functionality.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

Python Text File Analysis Functions

The document contains a series of Python programming tasks focused on reading from a text file named 'story.txt'. It includes functions to count letters, words, vowels, and specific lines, as well as to manipulate and create new files based on the content of 'story.txt'. Each task is presented with code snippets demonstrating the required functionality.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1. Write a program to read from “story.

txt” and display a count of Number of


i)lower case letters, ii)upper case letters, iii)words, iv)symbols.
with open("[Link]")as F:
L=U=S=0
W=1 #since the last word does not have a space or \n, counter
initialized to 1 and not 0
R= [Link]()
for k in R:
if [Link]():
if [Link](): L+=1
else: U +=1
elif k==' ' or k=='\n':W+=1
else :S+=1
print("Number of uppercase letters:", U)
print("Number of lowercase letters:", L)
print("Number of words:", W)
print("Number of symbols:", S)

2. Write a function COUNTTEXT( ), which reads a text file [Link] and


displays all the words whose length is more than 3 or those which start with
‘A’ or ‘a’
def COUNTTEXT():
with open("[Link]") as F:
R = [Link]().split()
for k in R:
if len(k) > 3 or k[0] in "aA":
print(k)

3. Write a function VOWEL_COUNT() which reads a text file “[Link]”


and prints the number of vowels in each line of that file.

#Using readline()
def VOWEL_COUNT():
L=1
with open("[Link]")as F:
while True:
X = [Link]()
if X == '':
break
Vowel=0
for k in X:
if [Link]() in "aeiou":
Vowel+=1
print("Number of vowels in Line ", L , "is: ", Vowel)
L +=1

#Using readlines()
def VOWEL_COUNT():
L=1
with open("[Link]")as F:
X=[Link]()
for line in X:
Vowel=0
for letter in line:
if [Link]() in "aeiou":
Vowel+=1
print("Number of vowels in Line ", L , "is: ", Vowel)
L +=1
4. Write a Python program that writes the reverse of each line in “[Link]” to
another text file “[Link]”. Display contents of the new file.
#Using readlines()
with open("[Link]") as F1, open("[Link]","w+") as F2:
X=[Link]()
for line in X:
[Link]([Link]()[::-1] + '\n') #to remove the end of line character(\n) ,
reverse the line & then attach the \n
[Link](0)
print([Link]())

#Using readline()
with open("[Link]") as F1, open("[Link]","w+") as F2:
while True:
X=[Link]()
if X == '':
break
[Link]([Link]()[::-1] + '\n')
[Link](0)
print([Link]())

5. Write a function BIGLINES() in Python to read lines from a text file “[Link]” , and
display those lines, which are bigger than 20 characters.

#Using readlines()
def BIGLINES():
with open("[Link]") as F:
X = [Link]()
for line in X:
if len(line) > 20:
print(line)

#Using readline()
def BIGLINES():
with open("[Link]") as F:
while True:
Line = [Link]()
if Line == '' :
break
if len(Line) > 20:
print(Line)

6. Write a function LONG_LINE() that reads from an existing “[Link]” and displays the
longest line
def LONG_LINE():
max=0
with open("[Link]") as F:
while True:
Line = [Link]()
if Line == '' :
break
if len(Line) >= max:
max = len(Line)
temp = Line
print("Longest line:", temp)
7. Write a function MAX_WORDS() that reads from an existing “[Link]” and returns the
line having the maximum number of words
def MAX_WORDS():
max=0
with open("[Link]") as F:
while True:
Line = [Link]()
if Line == '' :
break
if len([Link]()) >= max:
max = len([Link]())
temp = Line
print("line with :", max, "words : ", temp)
return temp

8. Write a function V_Count() which reads from “[Link]” and displays the number of
lines that start and end with a vowel.
def V_Count():
Count=0
with open("[Link]") as F:
X = [Link]()
for line in X:
line = [Link]() # to remove the end of line \n
if [Link]()[0] in "aeiou" and [Link]()[-2] in "aeiou":
Count+=1 #[-1] letter is a full stop . So -2 used
print("Number of lines starting and ending with vowel:", Count)

9. Write a program that reads from “[Link]” and creates another file “[Link]”
to have all the palindrome words of the original file. Display the contents of both the
files after the operation.
with open("[Link]") as F1, open("[Link]", "w+")as F2:
R=[Link]().split()
for word in R:
if [Link]() == [Link]()[::-1]:
[Link](word + '\n')
[Link](0)
[Link](0)
print("Original file:")
print([Link]())
print("New file:")
print([Link]())

10. Write a function REV_CASE() which reads from an existing “[Link]” and changes the
case of all occurances of ‘and’ from lower to uppercase ‘AND’. Also display the number
of such replacements made
def REV_CASE():
with open("[Link]","r+") as F:
X=[Link]()
for k in range(len(X)):
Y = X[k].split()
for j in range(len(Y)):
if Y[j]=='and':
Y[j] = "AND"
W= ' '.join(Y)+'\n'
X[k]=W
[Link](0)
[Link](X)
[Link](0)
print([Link]())
11. Write a function DISP_LINE() which displays those lines which start with a single letter.
def DISP_LINE():
with open("[Link]","r") as F:
X=[Link]()
for line in X:
if line[1] == ' ':
print(line)

You might also like