Introduction
Text processing is a core aspect of programming, and the ability to extract specific patterns from text files is a valuable skill. In this article, we’ll explore how to write a Python program to extract words that start with a vowel and end with a digit from a text file.
Getting Started
Let’s break down the process of achieving this task:
- Open the File: We’ll begin by opening the text file using the `open()` function.
- Read and Process Words: With the file open, we’ll read its contents word by word, splitting at spaces.
- Pattern Matching: For each word, we’ll check if it starts with a vowel (A, E, I, O, U, or their lowercase counterparts) and ends with a digit (0-9).
- Display Matching Words: If the word satisfies both conditions, we’ll display it.
Writing the Code
Let’s break down the process of achieving this task:
def extract_words(file_name):
try:
with open(file_name, 'r') as file:
vowels = 'aeiouAEIOU'
matching_words = []
for line in file:
if line:
words = line.split()
for word in words:
if word[0] in vowels and word[-1].isdigit():
matching_words.append(word)
return matching_words
except FileNotFoundError:
print("File not found.")
return []
if __name__ == "__main__":
file_name = input("Enter the name of the text file: ")
matching_words = extract_words(file_name)
if matching_words:
print("Words starting with a vowel and ending with a digit:")
for word in matching_words:
print(word)
else:
print("No matching words found.")
Understanding the Code
- The `extract_words()` function takes a filename as an argument.
- Within the function, we define the string `vowels` containing all vowel characters.
- We initialize an empty list called `matching_words` to store the words that satisfy the conditions.
- The program reads the file line by line, splitting each line into individual words.
- For each word, we check if its first character is a vowel and its last character is a digit. If both conditions are met, the word is added to the `matching_words` list.
- In the `__main__` block, the user is prompted to provide the name of the text file.
- The `extract_words()` function is called with the provided file name, and the matching words are stored in the `matching_words` list.
- If there are matching words, the program displays them. Otherwise, it notifies the user that no matching words were found.
Running the Program
- Save the code in a `.py` file (e.g., `vowel_digit_extractor.py`).
- Place the text file you want to analyze in the same directory as the Python script.
- Open a terminal and navigate to the directory containing the script.
- Run the script using the command: `python vowel_digit_extractor.py`.
- Follow the prompts to enter the name of the text file.
- The program will display the words that start with a vowel and end with a digit from the specified text file.
Conclusion
In this article, we’ve explored how to develop a Python program that extracts words starting with a vowel and ending with a digit from a text file. This process involves text file handling, string manipulation, and pattern matching, all of which are fundamental skills for any programmer working with textual data. This program serves as a practical example of how Python’s versatility can be harnessed to perform complex tasks efficiently.