Best way to strip punctuation from a string

前端 未结 26 1819
日久生厌
日久生厌 2020-11-21 05:39

It seems like there should be a simpler way than:

import string
s = \"string. With. Punctuation?\" # Sample string 
out = s.translate(string.maketrans(\"\",\         


        
26条回答
  •  你的背包
    2020-11-21 05:53

    Here is a function I wrote. It's not very efficient, but it is simple and you can add or remove any punctuation that you desire:

    def stripPunc(wordList):
        """Strips punctuation from list of words"""
        puncList = [".",";",":","!","?","/","\\",",","#","@","$","&",")","(","\""]
        for punc in puncList:
            for word in wordList:
                wordList=[word.replace(punc,'') for word in wordList]
        return wordList
    

提交回复
热议问题