python - Insert new line before multiple keywords in text file -


i have following text:

"this problem. extract data document. text looking for. not happening"

i able insert new line after multiple keywords:

import fileinput line in fileinput.input('text.txt', inplace=1):     print line,     if line.startswith(('extract',  'text')):         print ' ' 

but cannot find solution adding newline before keywords.

final format should like:

"this problem.

extract data document.

text looking for. not happening"

the issue words can change keywords. looking editing text containing keywords.

thanks suggestions.

do mean:

import fileinput line in fileinput.input('text.txt', inplace=1):     if line.startswith(('extract',  'text')):         print '\n'+line+'\n'         else:             print line 

?


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -