I think what I want to do is a fairly common task but I\'ve found no reference on the web. I have text with punctuation, and I want a list of the words.
\"H
I like the replace()
way the best. The following procedure changes all separators defined in a string splitlist
to the first separator in splitlist
and then splits the text on that one separator. It also accounts for if splitlist
happens to be an empty string. It returns a list of words, with no empty strings in it.
def split_string(text, splitlist):
for sep in splitlist:
text = text.replace(sep, splitlist[0])
return filter(None, text.split(splitlist[0])) if splitlist else [text]