My output (string):
First Line
second something aaa
MY2 asd hello
no one nothing
I need to remove all linesw before MY2
Output should looks line :
MY2 asd hello
no one nothing
code: (not working)
output= '\n'.join(output.split('\n'))
for line in output:
a=output.strip()=='MY2'
print(a)
You can traverse through all the lines, and keep the flag if string encountered.
output = """First Line
second something aaa
MY2 asd hello
no one nothing """
set_print = False
for line in output.split('\n'):
if line.startswith('MY2'):
set_print = True
if set_print:
print(line)
With itertools.dropwhile
feature:
from itertools import dropwhile
output = '''First Line
second something aaa
MY2 asd hello
no one nothing'''
for l in dropwhile(lambda s: not s.startswith('MY2'), output.splitlines()):
print(l)
The output:
MY2 asd hello
no one nothing
Another solution, using re
module ( regex101 ):
output = '''First Line
second something aaa
MY2 asd hello
no one nothing'''
import re
print( re.findall(r'^(MY2.*)', output, flags=re.S|re.M)[0] )
Prints:
MY2 asd hello
no one nothing
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.