Python Program to Print all the Pattern that Matches Given Pattern From a File


Finding lines in a file that match a particular pattern is a typical operation with many applications, such as log analysis, text processing, and data filtering. In this article, we will discuss the Python Program to Print all the Pattern that Matches Given Pattern From a File. To solve this problem, we will first create a pattern in a file to save it. Our task is to programmatically create the exact pattern that we see in the file. By applying some conditions it will check whether the pattern matches or not from a given file.

Syntax

with open("file_name.txt", "r") as file

The open() function work with ‘with’ statement to open the file. The open function accepts two parameters- file_name.txt to mention the file name and representing mode ‘r’ for opening the file for reading.

The file is the object type.

Algorithm

The following steps are −

  • The text file and make the pattern inside the file and save it.

  • We will start the program by creating the above pattern from the given file.

  • Then use the for loop to iterate through the range from 1 to 6 that creates 5 rows for the pattern design.

  • Next, print the pattern by multiplying ‘i’ with ‘*’ will create the exact pattern and match it to the text file.

  • We will check the program for pattern matching to the given file −

  • Then initialize the variable ‘contents’ to read the file by using the predefined function read().

  • Moving ahead to start the if-else statement where it applies the condition in the variable ‘content’- keyword aestrick(*) with the special character ‘\n’ to check whether the exact match of pattern found from the file or not.

  • If the exact matches are found then it will print that ‘Exact match found from the file’ otherwise no match found.

Example

In this program, we will create a pattern that is similar to the given file and check whether the pattern match from a file.

for i in range(1, 5):
   print("*"*i)
# Check the pattern for the exact matching
with open("pattern.txt", "r") as file:
   contents = file.read()
# special character of new line represented by \n.
   if "*\n**\n***\n****" in contents:
      print("Exact match found from file")
   else:
      print("Exact match not found.")

Output

*
**
***
****
*****
Exact match found!

Advantage

The following advantages of a pattern that matches the given pattern from a file −

  • Efficient data extraction − The program can extract specific information from large amounts of data such as log files or text documents fast and easily.

  • Data Analysis − The extracted information can be utilized for data analysis, text mining, and other applications in which specific information must be found among large amounts of data.

  • Built-in functions and libraries − Python allows the re-module that helps to build the pattern matching from the file.

  • Powerful and flexible − Python is an advanced and flexible programming language that enables you to effortlessly handle and analyse data in a variety of ways.

Conclusion

We understood the example of a pattern matched from a given file. We saw how mode r help to read the opening file and use the read() method to read the file. Then saw the importance of the special character ‘\n’ which provides a new line to match each row of the pattern(*).

Updated on: 01-Jun-2023

162 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements