Python Finds the String in the File and Print
Python is an effective programming language utilized for data analysis, ML/machine learning, web development, and automation. “Searching for a string” in a particular file and extracting it is a common programming task. We can search for a specific word, phrase, or pattern in a text file and extract it for further analysis. In this article, we will discuss the approaches to finding a string in a file and printing it in Python.
The following methods are used to find the string in the file:
- Using Python’s “Built-in File” Operations.
- Using “Regular Expressions”.
- Using the “read()” Method.
- Using the “readlines()” Method.
Method 1: Find the String in the File Using Python’s “Built-in File” Operations
To find a specific word, phrase, or pattern in a text file using Python, we can use some “built-in” file operations such as “open()”, “for” loop, and “if-else” statements. Here is an example:
The content in the original text file that will be dealt with is shown below:
The below code is utilized to locate the specified string and print it in Python:
for line in f:
if 'Python' in line:
print('String Not Found!')
In the above code:
- The “txt” is the name of the file that we want to search for the string, and “Python” is the string that we want to find.
- Now, open the file using the “open()” function and then go through each line in the file using a “for” loop.
- For each line, we can check if the string we’re looking for is there using a special word called “in“.
- Upon finding the string, print out the entire line.
Note: This method is simple and works well for small files. However, it may not be suitable for big files or when we need to perform complex string matching.
As analyzed, the string has been found and printed in the above output.
Method 2: Find the String in the File Using “Regular Expressions”
Regular expressions are like secret codes that we can utilize to find/search patterns in the specified text. The “re” library is utilized in Python to search for a string pattern by utilizing regular expressions.
The below code is used to find the string in the file and print it:
search_pattern = 'Python'
with open('filename.txt', 'r') as f:
for line in f:
match = re.search(search_pattern, line)
In the above code snippet:
- The “re” module is imported and a variable named “search_pattern” is initialized. This variable will be utilized to define the pattern of text to search for within the file.
- Next, the file is opened using the “open()” function, which automatically handles closing the file once it’s been read.
- The “for” loop goes over each line of text in the file. For each line, it uses the “search()” function to search for the pattern specified by the “search_pattern” variable.
- If the pattern is found in the line, the “match” variable is set to the match object returned by the “re.search()” function.
- Finally, if a match is found, the code prints the contents of the matched text via the “group()” method.
In the above output, the string has been located in accordance with the search pattern.
Method 3: Find the String in the File Using the “read()” Method
The “read()” method reads the full contents/data of a file into a string and the “find()” method is used to search for the string you want to find.
The following code will print the line number and line of the file where the specified string is found:
lines = f.read()
for i, line in enumerate(lines.splitlines()):
if "Python" in line:
print(i + 1, line)
In the above code lines:
- The “read()” method is utilized to read the entire/complete information of the file into a string variable called “lines”.
- The “enumerate()” function is used to get the line number as well as the line itself.
- The “if” statement is used to check if the word “Python” is in the line and in such a case, the line number and the line are printed to the console.
In this output, it can be seen that the string and its line number have been found and printed.
Method 4: Find the String in the File Using the “readlines()” Method
The “readlines()” method reads the contents of a file into a list of lines and the “for” loop is utilized to iterate through/over the list and search for the string you want to find.
The following code will print the line number and line of the file where the string is found:
lines = f.readlines()
for i, line in enumerate(lines):
if "Welcome" in line:
print(i + 1, line)
In this code block:
- The file named “filename.txt” is opened in read mode (“r”) and allocates the file object to the initialized variable “f”.
- A list of strings is created by reading the complete contents of the input file with the “readlines()” method.
- The “for” loop iterates over the lines in the file, one at a time, for each line, and the “enumerate()” function is applied to get the line number and the line itself.
- Now, the “if” statement is utilized to verify/check if the word “Welcome” is in the line.
- If the particular word is in the line, the code prints the line number and the line to the console.
In the above snippet, the string “Welcome” along with the complete line and line number has been returned appropriately.
This Python guide explored different ways to find a string in a file and print it in Python such as “built-in file” operations, “regular expressions”, “read()” method, or the “readlines()” method. This blog demonstrated the approaches to finding the string in the file and printing it.
Leave a Reply
You must be logged in to post a comment.