def strip_punctuation(text):
"""
strips the punctuation from a bunch of text
"""
# build a translation table for string.translate:
# there are other ways to do this:
# create a translation table to replace all punctuation with spaces
# -- then split() will remove the extra spaces
punctuation = string.punctuation
punctuation = punctuation.replace("'", "") # keep apostropies
punctuation = punctuation.replace("-", "") # keep hyphenated words
# building a translation table
table = {}
for c in punctuation:
table[ord(c)] = ' '
# remove punctuation with the translation table
text = text.translate(table)
# remove "--" -- can't do multiple characters with translate
text = text.replace("--", " ")
return text
评论列表
文章目录