Friday, December 27, 2024
Google search engine
HomeLanguagesPython | Finding ‘n’ Character Words in a Text File

Python | Finding ‘n’ Character Words in a Text File

This article aims to find words with a certain number of characters. In the code mentioned below, a Python program is given to find the words containing three characters in the text file.

Example

Input: Hello, how are you ? , n=3
Output: how are you
Explanation: Output contains every character of the of length 3 from the input file.

Input File: File1.txt

Finding 'n' Character Words in a Text File

File1.txt

Python Program to Find ‘n’ Character Words in a Text File

Below are the methods that we will cover in this article:

Finding a Given Word in Text File using Split and List Comprehension

You can read the file, split its content into words, and then filter out words with ‘n’ characters using list comprehension.

PYTHON




def find_words_with_three_chars(file_path):
    with open(file_path, 'r') as file:
        content = file.read()
        words = content.split()
 
        words_with_three_chars = [word for word in words if len(word) == 3]
 
        return words_with_three_chars
 
file_name = "File1.txt"
result = find_words_with_three_chars(file_name)
 
print("Words containing three characters:")
print(result)


Output:

Words containing three characters: ['sit', 'sed', 'sit', 'ac.', 'dui', 'sit']

Time Complexity: O(N), where N is the total number of characters in the file.
Space Complexity: O(N + M), where M is the number of words with ‘n’ characters in the file.

Search a given word in a Text File using Regular Expressions

In Python, we have re module which provides regular expression functionality. The Regular expressions can be used to match patterns in the text which allows you to find words with ‘n’ characters.

Python3




import re
 
def find_n_character_words(file_path, n):
    with open(file_path, 'r') as file:
        content = file.read()
 
    # Use regular expression to find words with 'n' characters
    pattern = r'\b\w{' + str(n) + r'}\b'
    words_with_n_chars = re.findall(pattern, content)
 
    return words_with_n_chars
 
file_name = "File1.txt"
n = 3
result = find_n_character_words(file_name, n)
 
print(f"Words containing {n} characters:")
print(result)


Output:

Words containing 3 characters:
['sit', 'sed', 'sit', 'dui', 'sit']

Time Complexity: O(N), where N is the total number of characters in the file.
Space Complexity: O(N)

Get Words Containing three Characters in the File using Generator Function

Here you can use a generator function to yield words with ‘n’ characters one by one. This can be useful for large files.

Python3




def generate_n_character_words(file_path, n):
    with open(file_path, 'r') as file:
        for line in file:
            for word in line.split():
                if len(word) == n:
                    yield word
 
file_name = "File1.txt"
n = 3
result_generator = generate_n_character_words(file_name, n)
 
print(f"Words containing {n} characters:")
for word in result_generator:
    print(word)


Output:

Words containing 3 characters:
sit
sed
sit
dui
sit

Time Complexity: O(N), where N is the total number of characters in the file.
Space Complexity: O(M), where M is the number of words with ‘n’ characters in the file.

RELATED ARTICLES

Most Popular

Recent Comments