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


Groups > comp.lang.python > #12996

Re: Python: Deleting specific words from a file.

From John Gordon <gordon@panix.com>
Newsgroups comp.lang.python
Subject Re: Python: Deleting specific words from a file.
Date 2011-09-09 03:16 +0000
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <j4c0ds$sv0$1@reader1.panix.com> (permalink)
References <30f9b718-bb3c-4c92-8a03-0f760c993939@a12g2000yqi.googlegroups.com>

Show all headers | View raw


In <30f9b718-bb3c-4c92-8a03-0f760c993939@a12g2000yqi.googlegroups.com> papu <prachar@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.

> fin=open(infile,"")

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
string.

Do this instead:

  line = 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.

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@panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"

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