Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.06; 'received:verizon.net': 0.07; 'terry': 0.07; 'line).': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'skip:[ 30': 0.09; 'fin.close()': 0.16; 'fin:': 0.16; 'fout': 0.16; 'fout.close()': 0.16; 'line.split()': 0.16; 'messy': 0.16; 'nl.': 0.16; 'preserved': 0.16; 'reedy': 0.16; 'result:': 0.16; 'scrub': 0.16; 'subject:Deleting': 0.16; 'written': 0.16; 'wrote:': 0.16; '>>>': 0.18; 'file)': 0.18; 'jan': 0.19; 'header:In-Reply-To:1': 0.22; 'pm,': 0.24; '(and': 0.29; 'example': 0.30; 'match': 0.30; 'list': 0.32; 'to:addr:python-list': 0.33; 'header:User-Agent:1': 0.34; '(as': 0.34; 'header:X-Complaints-To:1': 0.35; 'skip:" 20': 0.35; 'file': 0.36; 'doing': 0.36; 'skip:" 10': 0.36; 'like,': 0.37; 'run': 0.37; 'but': 0.37; 'received:org': 0.38; 'hello,': 0.38; 'should': 0.38; 'subject:: ': 0.39; 'header:Mime-Version:1': 0.39; 'data': 0.39; 'to:addr:python.org': 0.39; 'delete': 0.40; 'subject:from': 0.40; 'might': 0.40; 'your': 0.61; 'back': 0.62; 'here': 0.65; 'exact': 0.68; 'words).': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Python: Deleting specific words from a file. Date: Fri, 09 Sep 2011 02:04:15 -0400 References: <30f9b718-bb3c-4c92-8a03-0f760c993939@a12g2000yqi.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-74-109-121-73.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20110812 Thunderbird/6.0 In-Reply-To: <30f9b718-bb3c-4c92-8a03-0f760c993939@a12g2000yqi.googlegroups.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list 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: 43 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1315548327 news.xs4all.nl 2489 [2001:888:2000:d::a6]:51587 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:13003 On 9/8/2011 9:09 PM, 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() If you have very many words (and you will need all possible forms of each word if you do exact matches), The following (untested and incomplete) should run faster. delete_set = {"word_1","word_2"....,"word_n"} ... for line in fin: for word in line.split() if word not in delete_set: fout.write(word) # also write space and nl. Depending on what your file is like, you might be better with re.split('(\W+)', line). An example from the manual: >>> re.split('(\W+)', '...words, words...') ['', '...', 'words', ', ', 'words', '...', ''] so all non-word separator sequences are preserved and written back out (as they will not match delete set). -- Terry Jan Reedy