Python: Deleting specific words from a file.
gordon at panix.com
Fri Sep 9 05:16:13 CEST 2011
In <30f9b718-bb3c-4c92-8a03-0f760c993939 at a12g2000yqi.googlegroups.com> papu <prachar at gmail.com> writes:
> 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 = 
What does new_file do? I don't see it used anywhere.
There should be an "r" inside those quotes. In fact this is an error
and it will stop your program from running.
> fout = open(outfile,"w+")
What is the "+" supposed to do?
> for line in fin:
> for word in delete_list:
> line.replace(word, "")
replace() returns the modified string; it does not alter the existing
Do this instead:
line = line.replace(word, "")
> 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.
When you say you don't see the result file, do you mean it doesn't get
created at all?
John Gordon A is for Amy, who fell down the stairs
gordon at panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
More information about the Python-list