Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'subject:Python': 0.06; 'from:addr:python': 0.09; 'skip:[ 30': 0.09; 'fin.close()': 0.16; 'fin:': 0.16; 'fout': 0.16; 'fout.close()': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:name:mrab': 0.16; 'immutable,': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'messy': 0.16; 'received:84.92': 0.16; 'received:84.92.122': 0.16; 'received:84.92.122.60': 0.16; 'reply-to:addr:python-list': 0.16; 'result:': 0.16; 'scrub': 0.16; 'subject:Deleting': 0.16; 'wrote:': 0.16; 'file)': 0.18; 'result.': 0.21; 'header:In-Reply- To:1': 0.22; 'changed': 0.24; 'code': 0.25; 'received:84': 0.28; 'error': 0.32; 'list': 0.32; "can't": 0.33; 'to:addr:python-list': 0.33; 'header:User-Agent:1': 0.34; 'file.': 0.34; 'reply- to:addr:python.org': 0.34; 'skip:" 20': 0.35; 'file': 0.36; 'doing': 0.36; 'skip:" 10': 0.36; 'put': 0.37; 'but': 0.37; 'hello,': 0.38; 'subject:: ': 0.39; 'data': 0.39; 'to:addr:python.org': 0.39; 'subject:from': 0.40; 'here': 0.65; 'header:Reply-To:1': 0.71; 'reply-to:no real name:2**0': 0.71; '.replace': 0.84; 'words).': 0.84 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgYIABtraU7Unw4S/2dsb2JhbABCmTOMXoFzeIFGAQEFOEARCwgQCRQCDwkDAgECAQ04EwgBAYdzuFKGbQSLdUmMB4wE Date: Fri, 09 Sep 2011 02:31:23 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:6.0.1) Gecko/20110830 Thunderbird/6.0.1 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Python: Deleting specific words from a file. References: <30f9b718-bb3c-4c92-8a03-0f760c993939@a12g2000yqi.googlegroups.com> In-Reply-To: <30f9b718-bb3c-4c92-8a03-0f760c993939@a12g2000yqi.googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: python-list@python.org List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 27 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1315531942 news.xs4all.nl 2465 [2001:888:2000:d::a6]:53783 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:12987 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.