Search code examples
pythontextkeywordtext-extraction

Python: Get/Scan All Text After a Certain String


I have a text file which I read using readlines(). I need to start extracting data after a keyword in the text file. For example, after the key word Hello World below, I would like to retrieve the value 100 from Blah=100:

Blah=0 
Blah=2
Hello World
All the Text
 Will be Scan
And Relevant       
  Info will be
 Retrieved Blah=100

I can easily retrieved the information I want from the text file but I need it to start retrieving ONLY after a certain keyword in the textfile, such as after the 'Hello World' above. What I am currently doing is to retrieve the value using .split('='). Thus, I will retrieve all 3 values which are Blah=0, Blah=2 and Blah=100. I only wish to retrieve the value after a keyword in the text file, say 'Hello World', which is the value Blah=100.

There must be a simple way to do this. Please help. Thanks.


Solution

  • There are many ways to do it. Here's one:

    STARTER = "Hello World"
    FILENAME = "data.txt"
    TARGET = "Blah="
    
    with open(FILENAME) as f:
        value = None
        start_seen = False
        for line in f:
            if line.strip() == STARTER:
                start_seen = True
                continue
    
            if TARGET in line and start_seen:
                _,value = line.split('=')
                break
    
    if value is not None:
        print "Got value %d" % int(value)
    else:
        print "Nothing found"