Python find string in file and print 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. 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. Add a comment.

Active Oldest Votes. Improve this answer. Thanks a lot eyquem! Could you please be so kind to give me some hints about the meaning of the RE that you wrote? So I can modify it in case i need more than just the first line before or after in some cases the error has multiple lines,each one with a CRLF so with this RE i just get the first one.

Thanks a lot!!!! Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown.

The Overflow Blog. If stop is not specified, find starts at index start , and stops at the end of the string. For instance, the following statement searchs for "e" in mylines[0] , beginning at the fifth character.

In other words, starting at the 5th character in line[0], the first "e" is located at index 24 the "e" in "nec". If find doesn't locate the substring in the search range, it returns the number -1 , indicating failure:. But what if we want to locate every occurrence of a substring, not just the first one we encounter? We can iterate over the string, starting from the index of the previous match. In this example, we'll use a while loop to repeatedly find the letter "e". When an occurrence is found, we call find again, starting from a new location in the string.

Specifically, the location of the last occurrence, plus the length of the string so we can move forward past the last one. When find returns -1 , or the start index exceeds the length of the string, we stop.

The Python regular expressions module is called re. To use it in your program, import the module before you use it:. The re module implements regular expressions by compiling a search pattern into a pattern object. Methods of this object can then be used to perform match operations.

For example, let's say you want to search for any word in your document which starts with the letter d and ends in the letter r. What does this mean? So this regular expression will match any string that can be described as "a word boundary, then a lowercase 'd', then zero or more word characters, then a lowercase 'r', then a word boundary. To use this regular expression in Python search operations, we first compile it into a pattern object.

For instance, the following Python statement creates a pattern object named pattern which we can use to perform searches using that regular expression. The letter r before our string in the statement above is important. It tells Python to interpret our string as a raw string, exactly as we've typed it. Whenever you need Python to interpret your strings literally, specify it as a raw string by prefixing it with r. Now we can use the pattern object's methods, such as search , to search a string for the compiled regular expression, looking for a match.

If it finds one, it returns a special result called a match object. Otherwise, it returns None , a built-in Python constant that is used like the boolean value "false". To perform a case-insensitive search, you can specify the special constant re. So now we know how to open a file, read the lines into a list, and locate a substring in any given list element. Let's use this knowledge to build some example programs. The program below reads a log file line by line.

If the line contains the word "error," it is added to a list called errors. If not, it is ignored. The lower string method converts all strings to lowercase for comparison purposes, making the search case-insensitive without altering the original strings.

Note that the find method is called directly on the result of the lower method; this is called method chaining. The program below is similar to the above program, but using the re regular expressions module. The errors and line numbers are stored as tuples , e. The tuple is created by the additional enclosing parentheses in the errors. The elements of the tuple are referenced similar to a list, with a zero-based index in brackets. As constructed here, err[0] is a linenum and err[1] is the associated line containing an error.

The program below prints any line of a text file, info. This regex matches the following phone number notations:. The program below searches the dictionary for any words that start with h and end in pe. Make sure you're using Python 3 Reading data from a text file Using "with open" Reading text files line-by-line Storing text data in a variable Searching text for a substring Incorporating regular expressions Putting it all together.

Note In all the examples that follow, we work with the four lines of text contained in this file. Note Indentation is important in Python. Note If you're wondering why the index numbers start at zero instead of one, you're not alone. Tip This process is sometimes also called "trimming. Tip When you represent a string in your program with its literal contents, it's called a string literal. Note The letter r before our string in the statement above is important.

Related information Regular expressions quick reference. See our Python definition for additional help and information.



0コメント

  • 1000 / 1000