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
Post a Comment