Split Strings into words with multiple word boundary delimiters

前端 未结 30 2607
既然无缘
既然无缘 2020-11-21 05:09

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         


        
30条回答
  •  北海茫月
    2020-11-21 06:12

    def get_words(s):
        l = []
        w = ''
        for c in s.lower():
            if c in '-!?,. ':
                if w != '': 
                    l.append(w)
                w = ''
            else:
                w = w + c
        if w != '': 
            l.append(w)
        return l
    

    Here is the usage:

    >>> s = "Hey, you - what are you doing here!?"
    >>> print get_words(s)
    ['hey', 'you', 'what', 'are', 'you', 'doing', 'here']
    

提交回复
热议问题