Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #12996
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!news.glorb.com!solaris.cc.vt.edu!news.vt.edu!newsfeed-00.mathworks.com!panix!gordon |
|---|---|
| From | John Gordon <gordon@panix.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: Python: Deleting specific words from a file. |
| Date | Fri, 9 Sep 2011 03:16:13 +0000 (UTC) |
| Organization | PANIX Public Access Internet and UNIX, NYC |
| Lines | 51 |
| Message-ID | <j4c0ds$sv0$1@reader1.panix.com> (permalink) |
| References | <30f9b718-bb3c-4c92-8a03-0f760c993939@a12g2000yqi.googlegroups.com> |
| NNTP-Posting-Host | panix1.panix.com |
| X-Trace | reader1.panix.com 1315538173 29664 166.84.1.1 (9 Sep 2011 03:16:13 GMT) |
| X-Complaints-To | abuse@panix.com |
| NNTP-Posting-Date | Fri, 9 Sep 2011 03:16:13 +0000 (UTC) |
| User-Agent | nn/6.7.3 |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:12996 |
Show key headers only | 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 | Next — Previous in thread | Next in thread | Find similar | Unroll 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