Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #13197
| Date | 2011-09-12 21:42 +0100 |
|---|---|
| From | MRAB <python@mrabarnett.plus.com> |
| 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> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1046.1315860149.27778.python-list@python.org> (permalink) |
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 | Next — Previous 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