Skip to content

Instantly share code, notes, and snippets.

@floer32
Forked from kissgyorgy/remove_punc.py
Last active September 21, 2019 21:12
Show Gist options
  • Save floer32/b97aad3bc0f78af3b985bcbd2bb6b943 to your computer and use it in GitHub Desktop.
Save floer32/b97aad3bc0f78af3b985bcbd2bb6b943 to your computer and use it in GitHub Desktop.
Python: Remove punctuation from string (quickly)
#http://stackoverflow.com/questions/265960/best-way-to-strip-punctuation-from-a-string-in-python
import re, string
table = string.maketrans("","")
regex = re.compile('[%s]' % re.escape(string.punctuation))
def test_re(s): # From Vinko's solution, with fix.
return regex.sub('', s)
def test_trans(s):
return s.translate(table, string.punctuation)
#sets : 19.8566138744
#regex : 6.86155414581
#translate : 2.12455511093
#replace : 28.4436721802
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment