English

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 - Computer Science (Python)

Advertisements
Advertisements

Question

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

Short Note

Solution

def ETCount() :
     file = open ('TESTFILE.TXT', 'r')
     lines = file.readlines ()
     countE=0
     countT=0
     for w in lines :
        for ch in w:
             if ch in 'Ee':
                 countE = countE + 1
              if ch in 'Tt':
                  countT=countT + 1
print ("The number of E or e : ", countE)
print ("The number of T or t : ", countT)
file.close()
shaalaa.com
Reading from a Text File
  Is there an error in this question or solution?
2022-2023 (March) Sample

RELATED QUESTIONS

Which of the following statements are true?


readlines() method return ______.


Differentiate between readline() and readlines().


Write the use and syntax for the following method:

read()


Which of the following mode in the file opening statement results or generates an error if the file does not exist?


Write a method COUNTLINES() in Python to read lines from the text file ‘TESTFILE.TXT’ and display the lines which do not start 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


Write the definition of a Python function named LongLines() which reads the contents of a text file named 'LINES.TXT' and displays those lines from the file which have at least 10 words in it. For example, if the content of 'LINES.TXT' is as follows: 

Once upon a time, there was a woodcutter
He lived in a little house in a beautiful, green wood.
One day, he was merrily chopping some wood.
He saw a little girl skipping through the woods, whistling happily.
The girl was followed by a big gray wolf. 

Then the function should display output as:

He lived in a little house in a beautiful, green wood.
He saw a little girl skipping through the woods, whistling happily.


Write a function count_Dwords() in Python to count the words ending with a digit in a text file "Details.txt".

Example:
If the file content is as follows:
On seat2 V1P1 will sit and
On seat1 VVIP2 will be sitting
The output will be:
The number of words ending with a digit is 4


Write a function, lenWords(STRING), that takes a string as an argument and returns a tuple containing length of each word of a string.
For example, if the string is "Come let us have some fun", the tuple will have (4, 3, 2, 4, 4, 3)


Write a function in Python to read a text file, Alpha.txt and displays those lines which begin with the word ‘You’.


Write a function, vowelCount() in Python that counts and displays the number of vowels in the text file named Poem.txt.


Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×