Easy way to break up a sentence?
Padraig Brady
Padraig at Linux.ie
Wed Oct 2 09:54:58 EDT 2002
Ken wrote:
> Hi all, I am trying to do a simple word search engine. Is there an easy way
> to break up a sentence into individual words so that I can use it to compare
> without traversing through every character?
>
> Eg, something like this:
> from: "This is an example"
> to: ["This", "is", "an", "example"]
You can use "".split() but that will not
deal with punctuation. For that you will
need re.
import re
re.split('\W+', "This is an, example.")
This however will create an empty list item
for the last '.'
You can get around this by using the converse:
re.findall('\w+', "This is an, example.")
Pádraig.
More information about the Python-list
mailing list