简体   繁体   中英

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.

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"

Here's a slightly pseudo-codish answer- you just need a flag that changes to True once you've found the keyword:

thefile = open('yourfile.txt')

key = "Hello World"
key_found = False

for line in thefile:
    if key_found:
        get_value(line)
        # Optional: turn off key_found once you've found the value
        # key_found = False
    elif line.startswith(key):
        key_found = True

Here's one way, not necessarily the best; I hard-coded the text here, but you could use file.read() to get similar results:

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

keyword = 'Hello World'

lines = the_text.split('\n')
for line_num, line in enumerate(lines):
    if line.find(keyword) != -1:
        lines = lines[line_num:]
        break

the_value = None
value_key = 'Blah'
for line in lines:
    if line.find(value_key) != -1:
        the_value = line.split('=',2)[1]
        break

if the_value:
    print the_value

Example with regex.

reg = re.compile("Hello World")
data_re = re.ompile("Blah=(?P<value>\d)")
with open(f_name) as f:
   need_search = False
   for l in f:
       if reg.search(l) is not None:
          need_search = True
       if need_search == True:
          res = data_re.search(l)
          if res is not None:
             print res.groups('value')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM