In this post, we’ll explore the various methods used to search for a string in a text file. Using examples written in Python 3, we’ll demonstrate how to open and read both text files and CSV files. Once opened, we can search these files for specific strings.
Text files can hold many different types of data. It’s not uncommon to need to search these files for strings. The Python standard library includes many functions and modules that simplify the process of searching for strings in text files.
Search for a String in a Text File Using the readlines() Method
Before we can search for a string in a text file, we’ll need to read the file’s contents. The readlines() method reads a file’s data and return a list of the lines it contains. Each element in the list will contain a line from the text file.
In our first example, we’ll create a new text file and search it for a specific string. We’ll use the readlines() method to read the file’s data. Then we’ll assign that data to a variable called lines.
With the list of lines in hand, we can use a for loop to iterate through the text file and search for any lines containing the string “magician.” If one is found, the entire line is printed to the terminal.
Example: Find a string in a text file with readlines()
# create a new text file and write some text
text_file = open("text_file.txt",'w')
text_file.write("The magician appeared without a sound.")
text_file.close()
# safely open the text file we created and search for a string
with open("text_file.txt",'r') as text_file:
lines = text_file.readlines()
for line in lines:
if "magician" in line:
print(line)
Output
The magician appeared without a sound.
Search for a String in a Text File Using the read() Method
Another method we can use to read data from a text file in Python is read(). This method returns a string containing a file’s contents.
Using the read() method, we’ll search the following text file for the word “magician.” If the word we’re looking for is in the text file, the program will let us know by printing “True” to the console.
magic_story.txt
The magician appeared without a sound.
A nervous laugh escaped her lips. She heard the clock ticking
and realized the magician had changed his shape since the last
time she’d seen him. Julia felt her heart leap into her throat.
Example: Using the read() method to search a text file
story_file = open("magic_story.txt",'r')
story_text = story_file.read()
story_file.close()
if "magician" in story_text:
print("True")
Output
True
Count the Number of Occurrences of a String in a Text File
Next, using the story from the previous example, we’ll demonstrate how to count the number of times a word appears in a text file. Sometimes a text file contains many instances of a word or phrase. With Python, it’s possible to count how many times a string occurred in a text document.
In the following example, a Python with statement is used to open the text file and read its contents. Opening a file this way ensures that it is handled safely, and properly closed. With the file opened, the data is converted to a list of strings via the readlines() method.
Lastly, an f-string is used to print the results.
Example: Counting lines of text that contain a specific string
with open("magic_story.txt",'r') as text_file:
lines = text_file.readlines()
count = 0
for line in lines:
if "magician" in line:
count += 1
print(f"Found 'magician' {count} times.")
Output
Found 'magician' 2 times.
Find All the Lines Containing a String in a Text File
So far we’ve seen how to search a text file for a string. But what if we need more information? Suppose we want to record the lines of text that contain a string. How do we do this with Python?
Probably the easiest way to extract the lines from a file is with a list. For the next example, we’ll search a document for a string and add any lines that contain the string we’re looking for to a list using the append() method.
The append() method is used to add an element to a list. Once an element is added to a list, it can be accessed using brackets. By placing the index of the element inside brackets following the list variable, we can get a reference to that element.
We’ll also need to use the strip() method to remove the newline character from the end of each line. For this exercise, we’ll use the following text, an excerpt from T.S. Eliot’s The Hollow Men.
hollow_men.txt
Between the desire
And the spasm
Between the potency
And the existence
Between the essence
And the descent
Falls the Shadow
Example: Using a list to store lines of text
file = open("hollow_men.txt",'r')
lines = file.readlines()
file.close()
lines_we_want = []
for i in range(len(lines)):
if "Between" in lines[i]:
lines_we_want.append(lines[i].strip("\n"))
print(lines_we_want)
Output
['Between the desire', 'Between the potency', 'Between the essence']
Suggested Reading: If you are into machine learning, you can read this article on regression in machine learning. You might also like this article on k-means clustering with numerical example.
Using the find() Method to Search for a String in a Text File
The find() method returns the position of the first instance of a string. If the string isn’t found, this method returns a value of -1. We can use this method to check whether or not a file contains a string.
Example: Using the find() method
text = "Hello World"
print(text.find("Hello"))
print(text.find("World"))
print(text.find("Goodbye"))
Output
0
6
-1
Firstly, we can create a new text file with some example text. Secondly, we’ll open the file using a with statement. After reading the file’s contents, we’ll store the data as a new variable. Finally, using the find() method, we’ll attempt to locate the first instance of the word “magician” and print the results.
Example: Searching a text file for a string with the find() method
# create a new texts file and write some text
text_file = open("new_file.txt",'w')
text_file.write("The magician appeared without a sound.")
text_file.close()
# safely open the text file we created and search for a string
with open("new_file.txt",'r') as text_file:
text_data = text_file.read()
print(text_data.find("magician"))
Output
4
How to Search For String Data in a CSV File
A CSV file stores data as plain text. These files usually contain strings or numbers separated by a comma. That’s why we call them comma-separated value files. These CSV files make it easier for programs to exchange information.
Python comes with a module for reading CSV files. The csv module includes special methods for extracting data from these file types. One such method is reader().
Let’s assume our employee has handed us a file containing a list of user data for a website. Our job is to search this data for particular users. We’ll begin by searching for a user with the handle “the_magician.”
account_info.csv
Username,Email,Joined Date
cranky_jane,[email protected],01-21-2021
the_magician,[email protected],10-10-2020
sergeant_pepper,[email protected],05-15-2020
Before we can find the user’s handle, we’ll need to read the file data. By combining the reader() method with a for loop, it’s possible iterate over the rows of CSV data contained in the text file. If we come across a row containing the username we’re after, we’ll print the row’s data.
Example: Searching a CSV file for a string
import csv
# the string we want to find in the data
username = "the_magician"
with open("account_info.csv",'r') as data_file:
# create a reader object from the file data
reader = csv.reader(data_file)
# search each row of the data for the username
for row in reader:
if username in row:
print(row)
Output
['the_magician', '[email protected]', '10-10-2020']
What if we wanted to search the CSV file for more than one username? We can do this by creating a list of the usernames we want to search for. Comparing each row to the list of usernames makes it possible to locate the account data we’re trying to find.
Example: Search a CSV file for a list of strings
import csv
# the strings we want to find in the data
usernames = ["the_magician","cranky_jane"]
with open("account_info.csv",'r') as data_file:
# create a reader object from the file data
reader = csv.reader(data_file)
# search each row of the data for the username
for row in reader:
for username in usernames:
if username in row:
print(row)
Output
['cranky_jane', '[email protected]', '01-21-2021']
['the_magician', '[email protected]', '10-10-2020']
Conclusion
Completing a job is a lot easier when you have the proper tools. That’s why it’s important to learn about the many features of Python. In order to search for strings in text files, we needed to learn about methods like readlines() and find(). These are versatile methods that are great for solving many different problems.
Related Posts
If you’d like to learn more about Python development, follow these links to other excellent tutorials from Python for Beginners.
- Using Python read file methods to open a text file
- How to add a Python comment to a program
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.