Make lists of matching lines - several options:
def lines_that_equal(line_to_match, fp): return [line for line in fp if line == line_to_match] def lines_that_contain(string, fp): return [line for line in fp if string in line] def lines_that_start_with(string, fp): return [line for line in fp if line.startswith(string)] def lines_that_end_with(string, fp): return [line for line in fp if line.endswith(string)]
Create a consistent string generator (memory efficient):
def generate_lines_that_equal(string, fp): for line in fp: if line == string: yield line
Print all the relevant lines (first find all matches, then print them):
with open("file.txt", "r") as fp: for line in lines_that_equal("my_string", fp): print line
Print all the relevant lines (print them lazily, as we find them)
with open("file.txt", "r") as fp: for line in generate_lines_that_equal("my_string", fp): print line
Generators (created with the output) are your friends, especially with large files that do not fit into memory.
source share