如何从命令输出中搜索字符串,并从输出中打印下两行。
以下是代码:
a = """
Some lines I do not want
----- -------- --
I need this line
I need this line also
Again few lines i do not want
"""
for line in a.split("n"):
if line.startswith("----"):
print "I need this line"
print "I need this line also"
我在上面的代码中所做的是,我正在检查行是否以" ---"开头,这可以正常工作。现在,如何在行以" ---"开头后精确打印两行。在此示例代码打印中:"我需要这条线,我还需要此行"
您可以从列表中创建一个迭代器(文件句柄无需使用btw(。然后让for
迭代,但允许在循环中手动使用next
:
a = """
Some lines I do not want
----- -------- --
I need this line
I need this line also
Again few lines i do not want
"""
my_iter = iter(a.splitlines())
for line in my_iter:
if line.startswith("----"):
print(next(my_iter))
print(next(my_iter))
如果破折号之后没有足够的线路,则此代码将升高StopIteration
。避免此问题的一种替代方法是(礼貌乔恩·克莱门茨(
from itertools import islice
my_iter = iter(a.splitlines(True)) # preserves n (like file handle would do)
for line in my_iter:
if line.startswith("----"):
print(''.join(islice(my_iter, 2)))
另一种方式,不拆分字符串:
print(re.search("-----.*n(.*n.*)",a).group(1))
这将在 unsplitted ,多行字符串中的模式之后搜索2行。如果re.search
返回None
,可能会崩溃
在这两种情况下,您都会得到:
I need this line
I need this line also
这是一种简单的方法:
a = """
Some lines I do not want
----- -------- --
I need this line
I need this line also
Again few lines i do not want
"""
lines = a.split("n")
for i in range(len(lines)):
if lines[i].startswith("----"): # if current line starts with ----
print(lines[i+1]) # print next line.
print(lines[i+2]) # print line following next line.
# I need this line
# I need this line also
您可以存储当前行的索引,从而获取下一个n
行:
a = """
Some lines I do not want
----- -------- --
I need this line
I need this line also
Again few lines i do not want
"""
lines = a.split("n")
for index, line in enumerate(lines):
if line.startswith("----"):
print lines[index+1]
print lines[index+2]
您可能需要检查IndexError
S。
平原方式(几乎c(:
>>> a = """
Some lines I do not want
----- -------- --
I need this line
I need this line also
Again few lines i do not want
No nee
---- ------- --
Need this
And this
But Not this
"""
>>> start_printing, lines_printed = False, 0
>>> for line in a.split('n'):
if line.startswith('----'):
start_printing = True
elif start_printing:
print line
lines_printed += 1
if lines_printed>=2:
start_printing=False
lines_printed = 0
I need this line
I need this line also
Need this
And this
在这里列表综合的东西:
a = """
Some lines I do not want
----- -------- --
I need this line
I need this line also
-----------------------
A line after (---)
A consecutive line after (---)
"""
lines = a.split("n")
test = [print(lines[index+1] + 'n' + lines[index+2]) for index in range(len(lines)) if lines[index].startswith("----")]
#Output:I need this line
#I need this line also
#A line after (---)
#A consecutive line after (---)
我在进一步测试中碰到IndexError
,并增加了更多句子,因此我添加了一个例外块:
a = """
Some lines I do not want
----- -------- --
I need this line
I need this line also
-----------------------
A line after (---)
A consecutive line after (---)
-------------------------
Just for fun
Another one
-------------------------
"""
lines = a.split("n")
try:
test = [print(lines[index+1] + 'n' + lines[index+2]) for index in range(len(lines)) if lines[index].startswith("----")]
except:
pass
现在,所需的输出毫无例外:
I need this line
I need this line also
A line after (---)
A consecutive line after (---)
Just for fun
Another one