Python find string in file and print next line
The situation is as follows: Show
I have a .txt file with results of several nslookups. I want to loop tru the file and everytime it hits the string "Non-authoritative answer:" the scripts has to print the following 8 lines from that position. If it works I shoud get all the positive results in my screen :). First I had the following code:
But that only printed "Authoritative answers can be found from:" the times it was in the .txt. The code what I have now:
But when I run it, it prints the first result nicely to my screen but does not print the other positve results. p.s. I am aware of socket.gethostbyname("foobar.baz") but first I want to solve this basic problem. Thank you in advance! In this article, we are going to discuss, how to search for single or multiple strings in a file and get all the matched lines along with their line numbers. To check if a given string exists in the file or not, we have created a function, It accepts a file path and a string as
arguments. Then iterates over each line in the file one by one and for each line check if it contains the given string or not. If the line contains the given string, then return True. Whereas if no line in the file contains the given string, then it returns False. Contents of the file ‘sample.txt’ are, Let’s check if this file contains a string ‘is’ or not, Output: As file contains the ‘is’, therefore function
check_if_string_in_file() returns True. Here we get to know that file contains the given string or not. But what if we want to know all the exact occurrences of a string in the file like lines and line numbers. Let’s see how to do that, Advertisements we have created a function, to get all the lines and line numbers which contain the given string, def search_string_in_file(file_name, string_to_search): """Search for the given string in file and return lines containing that string, along with line numbers""" line_number = 0 list_of_results = [] # Open the file in read only mode with open(file_name, 'r') as read_obj: # Read all lines in the file one by one for line in read_obj: # For each line, check if line contains the string line_number += 1 if string_to_search in line: # If yes, then add the line number & line as a tuple in the list list_of_results.append((line_number, line.rstrip())) # Return list of tuples containing line numbers and lines where string is found return list_of_results It accepts a file path and a string as arguments. In the end, it returns a list of tuples, where each tuple contains the line number and line, which includes the given string. How did it worked ? Algorithm is as follows,
Suppose we have a file ‘sample.txt’ and its contents are, Hello this is a sample file It contains sample text Dummy Line A Dummy Line B Dummy Line C This is the end of file Let’s get all the line along with line numbers which contain the word ‘is’, matched_lines = search_string_in_file('sample.txt', 'is') print('Total Matched lines : ', len(matched_lines)) for elem in matched_lines: print('Line Number = ', elem[0], ' :: Line = ', elem[1]) Output: Total Matched lines : 2 Line Number = 1 :: Line = Hello this is a sample file Line Number = 6 :: Line = This is the end of file In total, there were two lines, which include the string ‘is’ and this function returned those lines along with their line numbers. Now suppose instead of search for a single string, we want to search for multiple strings in a file. Let’s see how to do that, Search for multiple strings in a file and get lines containing string along with line numbersTo search for multiple strings in a file, we can not use the above-created function because that will open and close the file for each string. Therefore, we have created a
separate function, that will open a file once and then search for the lines in the file that contains any of the given string i.e. def search_multiple_strings_in_file(file_name, list_of_strings): """Get line from the file along with line numbers, which contains any string from the list""" line_number = 0 list_of_results = [] # Open the file in read only mode with open(file_name, 'r') as read_obj: # Read all lines in the file one by one for line in read_obj: line_number += 1 # For each line, check if line contains any string from the list of strings for string_to_search in list_of_strings: if string_to_search in line: # If any string is found in line, then append that line along with line number in list list_of_results.append((string_to_search, line_number, line.rstrip())) # Return list of tuples containing matched string, line numbers and lines where string is found return list_of_results Let’s use this function, Contents of the file ‘sample.txt’ are, Hello this is a sample file It contains sample text Dummy Line A Dummy Line B Dummy Line C This is the end of file Let’s get all the lines along with their line numbers which either contain the word ‘is’ or ‘what’, # search for given strings in the file 'sample.txt' matched_lines = search_multiple_strings_in_file('sample.txt', ['is', 'what']) print('Total Matched lines : ', len(matched_lines)) for elem in matched_lines: print('Word = ', elem[0], ' :: Line Number = ', elem[1], ' :: Line = ', elem[2]) Output: Total Matched lines : 2 Word = is :: Line Number = 1 :: Line = Hello this is a sample file Word = is :: Line Number = 6 :: Line = This is the end of file The complete example is as follows, def check_if_string_in_file(file_name, string_to_search): """ Check if any line in the file contains given string """ # Open the file in read only mode with open(file_name, 'r') as read_obj: # Read all lines in the file one by one for line in read_obj: # For each line, check if line contains the string if string_to_search in line: return True return False def search_string_in_file(file_name, string_to_search): """Search for the given string in file and return lines containing that string, along with line numbers""" line_number = 0 list_of_results = [] # Open the file in read only mode with open(file_name, 'r') as read_obj: # Read all lines in the file one by one for line in read_obj: # For each line, check if line contains the string line_number += 1 if string_to_search in line: # If yes, then add the line number & line as a tuple in the list list_of_results.append((line_number, line.rstrip())) # Return list of tuples containing line numbers and lines where string is found return list_of_results def search_multiple_strings_in_file(file_name, list_of_strings): """Get line from the file along with line numbers, which contains any string from the list""" line_number = 0 list_of_results = [] # Open the file in read only mode with open(file_name, 'r') as read_obj: # Read all lines in the file one by one for line in read_obj: line_number += 1 # For each line, check if line contains any string from the list of strings for string_to_search in list_of_strings: if string_to_search in line: # If any string is found in line, then append that line along with line number in list list_of_results.append((string_to_search, line_number, line.rstrip())) # Return list of tuples containing matched string, line numbers and lines where string is found return list_of_results def main(): print('*** Check if a string exists in a file *** ') # Check if string 'is' is found in file 'sample.txt' if check_if_string_in_file('sample.txt', 'is'): print('Yes, string found in file') else: print('String not found in file') print('*** Search for a string in file & get all lines containing the string along with line numbers ****') matched_lines = search_string_in_file('sample.txt', 'is') print('Total Matched lines : ', len(matched_lines)) for elem in matched_lines: print('Line Number = ', elem[0], ' :: Line = ', elem[1]) print('*** Search for multiple strings in a file and get lines containing string along with line numbers ***') # search for given strings in the file 'sample.txt' matched_lines = search_multiple_strings_in_file('sample.txt', ['is', 'what']) print('Total Matched lines : ', len(matched_lines)) for elem in matched_lines: print('Word = ', elem[0], ' :: Line Number = ', elem[1], ' :: Line = ', elem[2]) if __name__ == '__main__': main() Output: *** Check if a string exists in a file *** Yes, string found in file *** Search for a string in file & get all lines containing the string along with line numbers **** Total Matched lines : 2 Line Number = 1 :: Line = Hello this is a sample file Line Number = 6 :: Line = This is the end of file *** Search for a multiple string in a file and get lines containing string along with line numbers *** Total Matched lines : 2 Word = is :: Line Number = 1 :: Line = Hello this is a sample file Word = is :: Line Number = 6 :: Line = This is the end of file How do you print the next line in a text file in Python?print normally appends another newline to the strings that it prints. To avoid the double newline, you can add end='' to the arguments of print . end is a keyword-only argument that determines the line ending that print appends to its output.
How do I find a specific string and print the whole line in Python?Approach:. Load the text file into the python program to find the given string in the file.. Ask the user to enter the string that you want to search in the file.. Read the text file line by line using readlines() function and search for the string.. After finding the string, print that entire line and continue the search.. How do I read a file and search string in Python?Here are the steps.. Open file in a read mode. Open a file by setting a file path and access mode to the open() function. ... . Read content from a file. Once opened, read all content of a file using the read() method. ... . Search for a string in a file. ... . Print line and line number.. How do you search for a string in a line in Python?Method 1: Finding the index of the string in the text file using readline() In this method, we are using the readline() function, and checking with the find() function, this method returns -1 if the value is not found and if found it returns 0.
|