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


Groups > comp.lang.python > #13197

Re: Python: Deleting specific words from a file.

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python@mrabarnett.plus.com>
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; 'terry': 0.07; "(i'm": 0.09; 'from:addr:python': 0.09; 'line).': 0.09; 'skip:[ 30': 0.09; 'am,': 0.12; "'r')": 0.16; 'character:': 0.16; '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; 'handy': 0.16; 'line.split()': 0.16; 'literals': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'messy': 0.16; 'nl.': 0.16; 'parentheses': 0.16; 'preserved': 0.16; 'received:84.92': 0.16; 'received:84.92.122': 0.16; 'received:84.92.122.60': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'reedy': 0.16; 'reply-to:addr:python- list': 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; 'sep': 0.23; 'pm,': 0.24; 'string': 0.26; 'received:84': 0.28; 'import': 0.28; '(and': 0.29; 'print': 0.29; 'example': 0.30; 'match': 0.30; 'list': 0.32; 'to:addr:python-list': 0.33; '...': 0.34; 'header:User-Agent:1': 0.34; '(as': 0.34; 'reply-to:addr:python.org': 0.34; 'skip:" 20': 0.35; 'file': 0.36; 'doing': 0.36; 'skip:" 10': 0.36; 'like,': 0.37; 'using': 0.37; 'run': 0.37; 'but': 0.37; 'hello,': 0.38; 'should': 0.38; 'subject:: ': 0.39; "there's": 0.39; 'recommended': 0.39; 'data': 0.39; 'to:addr:python.org': 0.39; 'case': 0.39; 'delete': 0.40; 'raw': 0.40; 'subject:from': 0.40; 'might': 0.40; 'your': 0.61; 'back': 0.62; 'here': 0.65; 'exact': 0.68; 'header:Reply-To:1': 0.71; 'reply-to:no real name:2**0': 0.71; 'words).': 0.84
X-IronPort-Anti-Spam-Filtered true
X-IronPort-Anti-Spam-Result AlMJADhubk5UXebj/2dsb2JhbABCmSKMV4F5eIFSAQEEAThABgsLCBAJFg8JAwIBAgENOBMIAQGHcwK2f4ZuBIt8SYwNjAg
Date Mon, 12 Sep 2011 21:42:21 +0100
From MRAB <python@mrabarnett.plus.com>
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:6.0.2) Gecko/20110902 Thunderbird/6.0.2
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> <mailman.898.1315548327.27778.python-list@python.org> <22e15921-d543-4604-8996-3bcb2770ef18@et6g2000vbb.googlegroups.com>
In-Reply-To <22e15921-d543-4604-8996-3bcb2770ef18@et6g2000vbb.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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1046.1315860149.27778.python-list@python.org> (permalink)
Lines 70
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1315860149 news.xs4all.nl 2512 [2001:888:2000:d::a6]:45107
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:13197

Show key headers only | View raw


On 12/09/2011 20:49, gry wrote:
> On Sep 9, 2:04 am, Terry Reedy<tjre...@udel.edu>  wrote:
>> 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
>
> re.sub is handy too:
> import re
> delete_list=('the','rain','in','spain')
> regex =  re.compile('\W' + '|'.join(delete_list) + '\W')

You need parentheses around the words (I'm using non-capturing
parentheses):

regex =  re.compile(r'\W(?:' + '|'.join(delete_list) + r')\W')

otherwise you'd get: '\Wthe|rain|in|spain\W'.

Even better is the word-boundary, in case there's no previous or next
character:

regex =  re.compile(r'\b(?:' + '|'.join(delete_list) + r')\b')

Raw string literals are recommended for regexes.

> infile='messy'
> with open(infile, 'r') as f:
>      for l in f:
>          print regex.sub('', l)

Back to comp.lang.python | Previous | NextPrevious 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