Last active
January 3, 2020 05:34
-
-
Save gwsu2008/57f74d2787aceac768dccff54141c7b9 to your computer and use it in GitHub Desktop.
Python search content in files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Build lists of matched lines - several flavors: | |
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)] | |
# Build generator of matched lines (memory efficient): | |
def generate_lines_that_equal(string, fp): | |
for line in fp: | |
if line == string: | |
yield line | |
# Print all matching lines (find all matches first, then print them): | |
with open("file.txt", "r") as fp: | |
for line in lines_that_equal("my_string", fp): | |
print line | |
# Print all matching 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment