Python Program to Count Vowels, Lines, and Characters in Text File


When working with text processing and analysis tasks, it is frequently required to count the vowels, lines, and characters in a text file. The goal is to determine the total counts of vowels, lines, and characters present in the file. Python provides various methods and techniques that can be used to accomplish these counting tasks effectively and efficiently.

In this article, we will discuss different approaches for counting vowels, lines, and characters in a text file using Python programming.

Approach

By following the below steps, we can effectively count the vowels, lines, and characters in a text file using Python.

  • Open the text file

  • Initialize counters

  • Read the file: Iterate over the file content, either by reading the entire file at once or line by line.

    • Reading the entire file at once: Use the read() method to read the entire content of the file as a single string.

    • Reading the file line by line: Use a for loop to iterate over each line in the file. The file object itself can be iterated directly.

  • Count vowels: For each line or the entire text, count the occurrences of vowels. You can use methods like count() or regular expressions to find the vowels and increment the vowel count accordingly.

  • Count lines: Increment the line count for each line read.

  • Count characters: Increment the character count based on the length of each line or the total text.

  • And finally, close the file.

Throughout this article, we will be using the following text file as our input.

Reading the entire file at once

Here, the entire content of the file is read using the read() method of the file object. The count() method is then used to count the occurrences of each vowel in the text, summing up the counts for all vowels. The same approach is used to count the number of newline characters (\n). The total number of characters is determined by calculating the length of the entire text string.

Example

Here's an example, that counts the number of vowels, lines, and characters in a text file.

def count_vowels_lines_chars(filename):
    with open(filename, 'r') as file:
        data = file.read()
        vowel_count = sum(data.count(vowel) for vowel in 'aeiouAEIOU')
        line_count = data.count('\n')
        char_count = len(data)

    return vowel_count, line_count, char_count

# Provide the path of the text file
filename = 'sample_document.txt'  

# Call the function to count vowels lines and characters
vowels, lines, characters = count_vowels_lines_chars(filename)

print("Vowels: {}".format(vowels))
print("Lines: {}".format(lines))
print("Characters: {}".format(characters))

Output

Vowels: 98
Lines: 3
Characters: 311

Using a for loop

This approach involves reading the file line by line using a for loop. For each line, the count() method is used to count the occurrences of vowels in that line. The counts are accumulated for all lines. The number of lines is incremented for each iteration of the loop. The total number of characters is determined by adding the length of each line.

Example

It works similarly to the previous example, but here we will be reading the file lines one by one using a for loop.

def count_vowels_lines_chars(filename):
    vowel_count = 0
    line_count = 0
    char_count = 0

    with open(filename, 'r') as file:
        for line in file:
            vowel_count += sum(line.count(vowel) for vowel in 'aeiouAEIOU')
            line_count += 1
            char_count += len(line)

    return vowel_count, line_count, char_count

# Provide the path of the text file
filename = 'sample_document.txt'  

# Call the function to count vowels lines and characters
vowels, lines, characters = count_vowels_lines_chars(filename)

print("Vowels: {}".format(vowels))
print("Lines: {}".format(lines))
print("Characters: {}".format(characters))

Output

Vowels: 98
Lines: 3
Characters: 311

Using the regular expressions

In this approach, the re.findall() function is used to find all occurrences of vowels in each line using a regular expression pattern [aeiouAEIOU]. The returned list of matches is then used to determine the count of vowels in that line. The counts are accumulated for all lines. The number of lines is incremented for each iteration of the loop. The total number of characters is determined by adding the length of each line.

Example

Here's an example of counting vowels, lines, and characters in the specified text file using regular expressions in Python.

import re

def count_vowels_lines_chars(filename):
    vowel_count = 0
    line_count = 0
    char_count = 0

    with open(filename, 'r') as file:
        for line in file:
            vowel_count += len(re.findall(r'[aeiouAEIOU]', line))
            line_count += 1
            char_count += len(line)

    return vowel_count, line_count, char_count


# Provide the path of the text file
filename = 'sample_document.txt'  

# Call the function to count vowels lines and characters
vowels, lines, characters = count_vowels_lines_chars(filename)

print("Vowels: {}".format(vowels))
print("Lines: {}".format(lines))
print("Characters: {}".format(characters))

Output

Vowels: 98
Lines: 3
Characters: 311

Updated on: 29-Aug-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements