I'm attempting to remove all lines where my regex matches(regex is simply looking for any line that has yahoo in it). Each match is on it's own line, so there's no need for the multiline option.
This is what I have so far...
import re
inputfile = open('C:\\temp\\Scripts\\remove.txt','w',encoding="utf8")
inputfile.write(re.sub("\[(.*?)yahoo(.*?)\n","",inputfile))
inputfile.close()
I'm receiving the following error:
Traceback (most recent call last): line 170, in sub return _compile(pattern, flags).sub(repl, string, count) TypeError: expected string or buffer
Use fileinput
module if you want to modify the original file:
import re
import fileinput
for line in fileinput.input(r'C:\temp\Scripts\remove.txt', inplace = True):
if not re.search(r'\byahoo\b', line):
print(line, end="")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With