Python: Deleting specific words from a file.

papu prachar at gmail.com
Thu Sep 8 21:09:31 EDT 2011


Hello, I have a data file (un-structed messy file) from which I have
to scrub specific list of words (delete words).

Here is what I am doing but with no result:

infile = "messy_data_file.txt"
outfile = "cleaned_file.txt"

delete_list = ["word_1","word_2"....,"word_n"]
new_file = []
fin=open(infile,"")
fout = open(outfile,"w+")
for line in fin:
    for word in delete_list:
        line.replace(word, "")
    fout.write(line)
fin.close()
fout.close()

I have put the code above in a file. When I execute it, I dont see the
result file. I am not sure what the error is. Please let me know what
I am doing wrong.



More information about the Python-list mailing list