Practical – 2 (Text files), 9 April
1 Write a UDF in Python named showInLines(), which reads the contents of a text file
named story.txt and displays every sentence in a separate line. Assume the sentence
ends with a “.” Or “!”.
For example:
Contents of story.txt:
Our parents told us that we must eat vegetables to be healthy. And it turns out, our
parents were right! So, what else did our parents tell?
OUTPUT:
Our parents told us that we must eat vegetables to be healthy.
it turns out, our parents were right!
So, what else did our parents tell?
2 Write a function c_words() in Python that separately counts and displays the number of
uppercase and lowercase alphabets in a text file “words.txt”
3 Write a function in Python to read a text file, Alpha.txt and displays those lines which
begin with the word ‘You’.
4 Write a function, vowel_count () in Python that counts and displays the number of
vowels in the text file named Poem.txt.
5 Write a method COUNTLINES() in Python to read lines from the text file ‘TESTFILE.TXT’
and display the lines which are not starting with any vowel.
Example:
If the file content is as follows:
An apple a day keeps the doctor away. We all pray for everyone’s safety. A marked
difference will come in our country.
The COUNTLINES() function should display the output as:
The number of lines not starting with any vowel – 1
6 Write a function ETCount() in Python, which should read each character of a text file
“TESTFILE.TXT” and then count and display the count of occurrence of alphabets E and T
individually (including small cases e and t too).
Example:
If the file content is as follows:
Today is a pleasant day. It might rain today. It is mentioned on weather sites
The ETCount() function should display the output as:
E or e: 6 T or t : 9