Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #12987

Re: Python: Deleting specific words from a file.

Date 2011-09-09 02:31 +0100
From MRAB <python@mrabarnett.plus.com>
Subject Re: Python: Deleting specific words from a file.
References <30f9b718-bb3c-4c92-8a03-0f760c993939@a12g2000yqi.googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.888.1315531942.27778.python-list@python.org> (permalink)

Show all headers | View raw


On 09/09/2011 02:09, papu wrote:
> 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.

The .replace method _returns_ its result.

Strings are immutable, they can't be changed in-place.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Python: Deleting specific words from a file. papu <prachar@gmail.com> - 2011-09-08 18:09 -0700
  Re: Python: Deleting specific words from a file. MRAB <python@mrabarnett.plus.com> - 2011-09-09 02:31 +0100
  Re: Python: Deleting specific words from a file. John Gordon <gordon@panix.com> - 2011-09-09 03:16 +0000
  Re: Python: Deleting specific words from a file. Terry Reedy <tjreedy@udel.edu> - 2011-09-09 02:04 -0400
    Re: Python: Deleting specific words from a file. gry <georgeryoung@gmail.com> - 2011-09-12 12:49 -0700
      Re: Python: Deleting specific words from a file. MRAB <python@mrabarnett.plus.com> - 2011-09-12 21:42 +0100

csiph-web