Python:在某个字符串后获取/扫描所有文本

时间:2022-08-22 13:12:23

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:

我有一个文本文件,我使用readlines()读取。我需要在文本文件中的关键字后面开始提取数据。例如,在关键字Hello World下面之后,我想从Blah = 100中检索值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.

我可以轻松地从文本文件中检索出我想要的信息,但我需要它才能在文本文件中的某个关键字之后开始检索,例如在上面的“Hello World”之后。我目前正在做的是使用.split('=')检索值。因此,我将检索所有3个值,即Blah = 0,Blah = 2和Blah = 100。我只希望在文本文件中的关键字后面检索值,比如'Hello World',这是值Blah = 100。

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

必须有一个简单的方法来做到这一点。请帮忙。谢谢。

4 个解决方案

#1


1  

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"

#2


0  

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

这是一个稍微伪编码的答案 - 你只需要一个标志,一旦找到关键字就会变为True:

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

#3


0  

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

这是一种方式,不一定是最好的方式;我在这里对文本进行了硬编码,但您可以使用file.read()来获得类似的结果:

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

#4


0  

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')

#1


1  

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"

#2


0  

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

这是一个稍微伪编码的答案 - 你只需要一个标志,一旦找到关键字就会变为True:

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

#3


0  

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

这是一种方式,不一定是最好的方式;我在这里对文本进行了硬编码,但您可以使用file.read()来获得类似的结果:

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

#4


0  

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')