I am trying to figure out how to read a file, find specific substrings and edit the entered file to write characters before and after this substring, but I'm stuck. I can only understand how to write to the end of the file, and not in the middle of the file in the middle of the line somewhere!
So, for example, let's say I have a text file:
blah blurh blap
then I have the code:
f = open('inputFile.txt', 'r+')
for line in f:
if 'blah' in line:
f.write('!')
f.close()
As it is written above, as a result, the text will say something like:
blah blurh blap!
but I need to figure out how to say this:
!blah! blurh blap
and I can't figure it out and can't find anything on the Internet about it. Any ideas?
source
share