strip punctuation with regex - python

前端 未结 3 2065
太阳男子
太阳男子 2020-12-09 03:41

I need to use regex to strip punctuation at the start and end of a word. It seems like regex would be the best option for this. I don\'t want punctuation r

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-09 04:33

    I think this function will be helpful and concise in removing punctuation:

    import re
    def remove_punct(text):
        new_words = []
        for word in text:
            w = re.sub(r'[^\w\s]','',word) #remove everything except words and space#how 
                                            #to remove underscore as well
            w = re.sub(r'\_','',w)
            new_words.append(w)
        return new_words
    

提交回复
热议问题