Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete a specific string (not line) from a text file python

Tags:

python

I have a text file with two lines in a text file:

<BLAHBLAH>483920349<FOOFOO>
<BLAHBLAH>4493<FOOFOO>

Thats the only thing in the text file. Using python, I want to write to the text file so that i can take away BLAHBLAH and FOOFOO from each line. It seems like a simple task but after refreshing my file manipulation i cant seem to find a way to do it. Help is greatly appreciated :)

Thanks!

like image 878
Tachyon Avatar asked Oct 14 '25 08:10

Tachyon


1 Answers

If it's a text file as you say, and not HTML/XML/something else, just use replace:

for line in infile.readlines():
    cleaned_line = line.replace("BLAHBLAH","")
    cleaned_line = cleaned_line.replace("FOOFOO","")

and write cleaned_line to an output file.

like image 154
John Lyon Avatar answered Oct 16 '25 22:10

John Lyon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!