{"id":5040,"date":"2023-10-26T15:53:08","date_gmt":"2023-10-26T10:23:08","guid":{"rendered":"https:\/\/python-programs.com\/?p=5040"},"modified":"2023-11-10T12:00:20","modified_gmt":"2023-11-10T06:30:20","slug":"python-search-strings-in-a-file-and-get-line-numbers-of-lines-containing-the-string","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-search-strings-in-a-file-and-get-line-numbers-of-lines-containing-the-string\/","title":{"rendered":"Python: Search strings in a file and get line numbers of lines containing the string"},"content":{"rendered":"
In this article we will discuss how to search strings in a file and get all the lines and line numbers which will match or which contains the string.<\/p>\n
Searching string in a file is easily accomplished by python. And we can easily get the line numbers of lines containing the string.<\/p>\n
First take a file named Let’s see the program for it.<\/p>\n Here, we use loop for iteration to check each line whether the string is present or not. If the line contains the string then it will return True and if the line does not contain the string then it will return False.<\/p>\n Suppose we have a file named Here, we use loop for iteration to check each line whether the string is present or not. If the line contains the string then it will return True and if the line does not contain the string then it will return False.<\/p>\n We tried to print the total number of matched lines which consist of the string \u2018is\u2019. In total, there where two lines, which include the string \u2018is\u2019 and this function returned those lines along with their line numbers. Now instead of searching single string we want to search multiple string.<\/p>\n To search for multiple string in a file, we have to create a separate function, that will open a file once and then search for the lines in the file which contains the given string. Because we cannot use the above created function because this will open and close the file for each string.<\/p>\n Suppose we have a file named \u201cexample.txt\u201d<\/p>\n Here, we use loop for iteration to check each line whether the string is present or not. If the line contains the string then it will return True and if the line does not contain the string then it will return False.<\/p>\n","protected":false},"excerpt":{"rendered":" Search strings in a file and get line numbers of lines containing the string in Python. In this article we will discuss how to search strings in a file and get all the lines and line numbers which will match or which contains the string. Searching string in a file is easily accomplished by python. …<\/p>\n\u201cexample.txt\u201d<\/code><\/p>\n
This is an example for sample file\r\nProgramming needs logic.\r\nLanguages are of many types.\r\nComputer science is a study of computers & computational systems.\r\nWe can write a program in any language.\r\nThe end<\/pre>\n
#Program :\r\n\r\n# create a function to check the string is present in the file or not\r\ndef string_in_file(file_name, string_to_search):\r\n\r\n #Checking if any line in the metioned file contains given string or not\r\n # Open the file in read only mode to read content of the file\r\n with open(file_name, 'r') as read_obj:\r\n\r\n # Reading all lines in the file one by one by iteration\r\n for line in read_obj:\r\n # For each line, checking if the line contains the string or not\r\n if string_to_search in line:\r\n return True\r\n return False\r\n\r\n\r\n\r\n#checking if string 'is' is found in file 'sample.txt'\r\nif string_in_file('sample.txt','is'):\r\n print('string found')\r\nelse:\r\n print('string not found')<\/pre>\n
Output:\r\nstring found<\/pre>\n
Search for a string in file & get all lines containing the string along with line numbers :<\/h3>\n
\u201cexample.txt\u201d<\/code><\/p>\n
This is an example for sample file\r\nProgramming needs logic.\r\nLanguages are of many types.\r\nComputer science is a study of computers & computational systems.\r\nWe can write a program in any language.\r\nThe end<\/pre>\n
#Program :\r\n\r\ndef string_in_file(file_name, string_to_search):\r\n #Searching for the given string in file along with its line numbers\r\n line_number = 0\r\n list_of_results = []\r\n # Opening the file in read only mode\r\n with open(file_name, 'r') as read_obj:\r\n # Reading all lines in the file one by one by iterating the file\r\n for line in read_obj:\r\n # checking each line, if the line contains the string\r\n line_number += 1\r\n if string_to_search in line:\r\n # If it contains the string, then add the line number & line as a tuple in the list\r\n list_of_results.append((line_number, line.rstrip()))\r\n # Return list of tuples containing line numbers and lines where string is found\r\n return list_of_results\r\n\r\n\r\nlines = string_in_file('example.txt', 'is')\r\nprint('Total Matched lines : ', len(lines))\r\nfor i in lines:\r\n print('Line Number = ', i[0], ' :: Line = ', i[1])\r\n<\/pre>\n
Output:\r\nTotal Matched lines : 2\r\nLine Number =\u00a0 1\u00a0 :: Line =\u00a0 This is an example for sample file\r\nLine Number =\u00a0 4\u00a0 :: Line =\u00a0 Computer science is a study of computers & computational systems.<\/pre>\n
Search for multiple strings in a file and get lines containing string along with the line numbers :<\/h3>\n
This is an example for sample file\r\nProgramming needs logic.\r\nLanguages are of many types.\r\nComputer science is a study of computers & computational systems.\r\nWe can write a program in any language.\r\nThe end<\/pre>\n
#Program :\r\n\r\ndef strings_in_file(file_name, list_of_strings):\r\n #Here getting line from the file along with line numbers\r\n #which contains any of the matching string from the list\r\n line_number = 0\r\n list_of_results = []\r\n # Opening the file in read only mode\r\n with open(file_name, 'r') as read_obj:\r\n # Reading all lines in the file one by one by iteration\r\n for line in read_obj:\r\n line_number += 1\r\n # Checking each line, if the line contains any string from the list of strings\r\n for string_to_search in list_of_strings:\r\n if string_to_search in line:\r\n # If any string is matched\/found in line\r\n # then we will append that line along with line number in the list\r\n list_of_results.append((string_to_search, line_number, line.rstrip()))\r\n # Returning the list of tuples containing matched string, line numbers and lines where string is found\r\n return list_of_results\r\n\r\n#Now, we will use this function\r\n\r\n# search for given strings in the file 'sample.txt'\r\nmatched_lines = strings_in_file('sample.txt', ['is', 'what'])\r\nprint('Total Matched lines : ', len(matched_lines))\r\nfor elem in matched_lines:\r\n print('Word = ', elem[0], ' :: Line Number = ', elem[1], ' :: Line = ', elem[2])\r\n\r\n<\/pre>\n
Output:\r\nTotal Matched lines : 2\r\nWord = 'is' :: Line Number =\u00a0 1\u00a0 :: Line =\u00a0 This is an example for sample file\r\nWord = 'is' :: Line Number =\u00a0 4\u00a0 :: Line =\u00a0 Computer science is a study of computers & computational systems.<\/pre>\n