Finding string in file python

Python: Search strings in a file and get line numbers of lines containing the string

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.

Check if a string exists in a file

To check if a given string exists in the file or not, we have created a function,

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

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.

Читайте также:  Php как ограничить время выполнения

Contents of the file ‘sample.txt’ are,

Frequently Asked:

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 check if this file contains a string ‘is’ or not,

# 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')

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,

Search for a string in file & get all lines containing the string along with line numbers

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 ?

  • Accept arguments – file path and a string to lookup.
  • Create an empty list of tuples.
  • Open the file at the given path in read-only mode.
  • Iterates over each line in the file one by one.
    • For each line, check if it contains the given string or not.
      • If the line contains the given string,
        • Creates a tuple of line number & the line and adds that to a list of tuples.

        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])
        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 numbers

        To 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

        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])
        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()
        *** 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

        Источник

        Python Find String in File

        Python Find String in File

        1. Use the File readlines() Method to Find a String in a File in Python
        2. Use the File read() Method to Search a String in a File in Python
        3. Use find Method to Search a String in a File in Python
        4. Use mmap Module to Search a String in a File in Python

        The tutorial explains how to find a specific string in a text file in Python.

        Use the File readlines() Method to Find a String in a File in Python

        Pyton file readlines() method returns the file content split to a list by the new line. We can use the for loop to iterate through the list and use the in operator to check whether the string is in the line in every iteration.

        If the string is found in the line, it returns True and breaks the loop. If the string is not found after iterating all lines, it returns False eventually.

        An example code for this approach is given below:

        file = open("temp.txt", "w") file.write("blabla is nothing.") file.close();  def check_string():  with open('temp.txt') as temp_f:  datafile = temp_f.readlines()  for line in datafile:  if 'blabla' in line:  return True # The string is found  return False # The string does not exist in the file  if check_string():  print('True') else:  print('False') 

        Use the File read() Method to Search a String in a File in Python

        The file read() method returns the content of the file as a whole string. Then we can use the in operator to check whether the string is in the returned string.

        An example code is given below:

        file = open("temp.txt", "w") file.write("blabla is nothing.") file.close();  with open('temp.txt') as f:  if 'blabla' in f.read():  print("True") 

        Use find Method to Search a String in a File in Python

        A simple find method can be used with the read() method to find the string in the file. The find method is passed the required string. It returns 0 if the string is found and -1 if the string is not found.

        An example code is given below.

        file = open("temp.txt", "w") file.write("blabla is nothing.") file.close();  print(open('temp.txt', 'r').read().find('blablAa')) 

        Use mmap Module to Search a String in a File in Python

        The mmap module can also be used to find a string in a file in Python and can improve the performance if the file size is relatively big. The mmap.mmap() method creates a string-like object in Python 2 that checks the implicit file only and does not read the whole file.

        An example code in Python 2 is given below:

        # python 2  import mmap file = open("temp.txt", "w") file.write("blabla is nothing.") file.close();  with open('temp.txt') as f:  s = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)  if s.find('blabla') != -1:  print('True') 

        However, in Python3 and above, mmap doesn’t behave like the string-like object but creates a bytearray object. So the find method looks for bytes and not for strings.

        An example code for this is given below:

        import mmap file = open("temp.txt", "w") file.write("blabla is nothing.") file.close();  with open('temp.txt') as f:  s = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)  if s.find(b'blabla') != -1:  print('True') 

        Syed Moiz is an experienced and versatile technical content creator. He is a computer scientist by profession. Having a sound grip on technical areas of programming languages, he is actively contributing to solving programming problems and training fledglings.

        Related Article — Python String

        Related Article — Python File

        Copyright © 2023. All right reserved

        Источник

Оцените статью