Python is an efficient programming language utilized for knowledge evaluation, ML/machine studying, net growth, and automation. “Looking for a string” in a selected file and extracting it’s a frequent programming job. We will seek for a particular phrase, phrase, or sample in a textual content file and extract it for additional evaluation. On this article, we are going to focus on the approaches to discovering a string in a file and printing it in Python.
The next strategies are used to seek out the string within the file:
Methodology 1: Discover the String within the File Utilizing Python’s “Constructed-in File” Operations
To discover a particular phrase, phrase, or sample in a textual content file utilizing Python, we are able to use some “built-in” file operations corresponding to “open()”, “for” loop, and “if-else” statements. Right here is an instance:
The content material within the unique textual content file that can be handled is proven beneath:
Instance
The beneath code is utilized to find the required string and print it in Python:
with open(‘filename.txt’, ‘r’) as f:
for line in f:
if ‘Python’ in line:
print(line)
else:
print(‘String Not Discovered!’)
Within the above code:
- The “txt” is the title of the file that we need to seek for the string, and “Python” is the string that we need to discover.
- Now, open the file utilizing the “open()” perform after which undergo every line within the file utilizing a “for” loop.
- For every line, we are able to test if the string we’re in search of is there utilizing a particular phrase known as “in“.
- Upon discovering the string, print out your complete line.
Word: This methodology is easy and works nicely for small recordsdata. Nonetheless, it is probably not appropriate for giant recordsdata or when we have to carry out advanced string matching.
Output
As analyzed, the string has been discovered and printed within the above output.
Methodology 2: Discover the String within the File Utilizing “Common Expressions”
Common expressions are like secret codes that we are able to make the most of to seek out/search patterns within the specified textual content. The “re” library is utilized in Python to seek for a string sample by using common expressions.
Instance
The beneath code is used to seek out the string within the file and print it:
import re
search_pattern = ‘Python’
with open(‘filename.txt’, ‘r’) as f:
for line in f:
match = re.search(search_pattern, line)
if match:
print(match.group())
Within the above code snippet:
- The “re” module is imported and a variable named “search_pattern” is initialized. This variable can be utilized to outline the sample of textual content to seek for throughout the file.
- Subsequent, the file is opened utilizing the “open()” perform, which routinely handles closing the file as soon as it’s been learn.
- The “for” loop goes over every line of textual content within the file. For every line, it makes use of the “search()” perform to seek for the sample specified by the “search_pattern” variable.
- If the sample is discovered within the line, the “match” variable is about to the match object returned by the “re.search()” perform.
- Lastly, if a match is discovered, the code prints the contents of the matched textual content by way of the “group()” methodology.
Output
Within the above output, the string has been situated in accordance with the search sample.
Methodology 3: Discover the String within the File Utilizing the “learn()” Methodology
The “learn()” methodology reads the complete contents/knowledge of a file right into a string and the “discover()” methodology is used to seek for the string you need to discover.
Instance
The next code will print the road quantity and line of the file the place the required string is discovered:
with open(“filename.txt”, “r”) as f:
traces = f.learn()
for i, line in enumerate(traces.splitlines()):
if “Python” in line:
print(i + 1, line)
Within the above code traces:
- The “learn()” methodology is utilized to learn your complete/full data of the file right into a string variable known as “traces”.
- The “enumerate()” perform is used to get the road quantity in addition to the road itself.
- The “if” assertion is used to test if the phrase “Python” is within the line and in such a case, the road quantity and the road are printed to the console.
Output
On this output, it may be seen that the string and its line quantity have been discovered and printed.
Methodology 4: Discover the String within the File Utilizing the “readlines()” Methodology
The “readlines()” methodology reads the contents of a file into an inventory of traces and the “for” loop is utilized to iterate by/over the listing and seek for the string you need to discover.
Instance
The next code will print the road quantity and line of the file the place the string is discovered:
with open(“filename.txt”, “r”) as f:
traces = f.readlines()
for i, line in enumerate(traces):
if “Welcome” in line:
print(i + 1, line)
On this code block:
- The file named “filename.txt” is opened in learn mode (“r”) and allocates the file object to the initialized variable “f”.
- An inventory of strings is created by studying the entire contents of the enter file with the “readlines()” methodology.
- The “for” loop iterates over the traces within the file, one after the other, for every line, and the “enumerate()” perform is utilized to get the road quantity and the road itself.
- Now, the “if” assertion is utilized to confirm/test if the phrase “Welcome” is within the line.
- If the actual phrase is within the line, the code prints the road quantity and the road to the console.
Output
Within the above snippet, the string “Welcome” together with the entire line and line quantity has been returned appropriately.
Conclusion
This Python information explored alternative ways to discover a string in a file and print it in Python corresponding to “built-in file” operations, “common expressions”, “learn()” methodology, or the “readlines()” methodology. This weblog demonstrated the approaches to discovering the string within the file and printing it.