简体   繁体   English

提取所有行,包括包含子字符串的行以及python中子字符串之后的行

[英]Extract all lines including the line which contains the substring and lines after the substring in python

I am running into problems where I am trying to extract the lines after including the line from where the substring exist. 在遇到包含子字符串的行之后,我试图提取行时遇到了问题。

s="""
   This is so awesome
   I need to do this more often
   This forum rocks
   Help me
  """

If the substring I search is forum , I want to get the result as 如果我搜索的子字符串是forum ,则我希望得到的结果为

   this forum rocks
   Help me

I tried using the following statement 我尝试使用以下语句

s.lower().split("forum",1)[1]

and my output is 我的输出是

forum rocks

Any help is appreciated. 任何帮助表示赞赏。

You'll want to split the string by line, and search each line for the word you want. 您将需要按行分割字符串,并在每一行中搜索所需的单词。

s="""
This is so awesome
I need to do this more often
This forum rocks
Help me
""".split('\n')
for line in range(len(s)):
    if "forum" in s[line]:
        print(s[line])
        print(s[line+1])

As long as the multi-line string ends on the next line after the last line with text in it, you won't go out of bounds for the list. 只要多行字符串在包含文本的最后一行之后的下一行结束,您就不会超出列表的范围。 If you have the last """ on the previous line, next to Help me , you'll have to do a range check. 如果上一行的最后一个"""位于“ Help me旁边,则必须进行范围检查。

EDIT: Re-read the question. 编辑:重新阅读问题。 You want all lines after the word forum is found? 您想在找到论坛一词后排所有行吗? The previous example I gave just gets you the next line. 我给出的上一个示例仅使您获得下一行。 For all lines after the key word is found, use this: 对于找到关键字后的所有行,请使用以下命令:

s="""
This is so awesome
I need to do this more often
This forum rocks
Help me
""".split('\n')
found = False
for line in range(len(s-1)):
    if "forum" in s[line] or found:
        print(s[line])
        found = True

The len(s-1) part is optional. len(s-1)部分是可选的。 Depending on whether you want the trailing blank line included in the results. 取决于是否要在结果中包含尾随空白行。 If you want that last blank line, just change it back to len(s) . 如果要最后一行空行,只需将其更改回len(s)

Try this, It will work for a string containing any number of lines. 试试这个,它将适用于包含任意行的字符串。

s="""
   This is so awesome
   I need to do this more often
   This forum rocks
   Help me
  """
s=s.split('\n')
c=0
for i in s:
    if i.find("forum")!=-1: #  no match, find returns -1
        print "\n".join(s[c:])
    c+=1

Output : 输出:

This forum rocks
Help me

So, basically you find the index in the array where your match has been found and then return everything after that (by joining with a \\n as was the case in the original string). 因此,基本上,您会在找到匹配项的数组中找到索引,然后返回所有内容(与原始字符串一样,通过加入\\n )。

One-line solution with re.search() function: 使用re.search()函数的单行解决方案:

import re

s="""
   This is so awesome
   I need to do this more often
   This forum rocks
   Help me
  """    
result = re.search(r'.*\bforum[\s\S]*', s, re.M).group()
print(result)

The output: 输出:

   This forum rocks
   Help me
l = s.split('\n')
for n, str in enumerate(l):
    if 'forum' in str:
        print ('\n'.join(l[n:]))
        break

Output: 输出:

   This forum rocks
   Help me

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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