Search code examples
pythonperformancefiletext-fileslarge-files

Getting a line in a large file's content


I am wondering how to implement Aaron Digulla's answer in this question: Fastest Text search method in a large text file

with open ('test.txt', 'rt') as myfile:
    contents = myfile.read() 
    match = re.search("abc", contents)

What's next, so that I can find the previous EOL and next EOL, and so that I can extract the line?


Solution

  • Replace

    match = re.search("abc", contents)
    

    with

    match = re.search("^.*abc.*$", contents, re.M)
    

    It will match the whole line containing "abc". Used with re.M flag ^ matches the beginning of a line and $ its end.

    Here is an example:

    import re
    
    s = """
    Twinkle, twinkle
    Little star!
    How I wonder 
    What you are!
    """
    
    term = "star"
    match = re.search(f"^.*{term}.*$", s, re.M)
    print(match.group(0))
    

    It gives:

    Little star!