-
-
Save floer32/b97aad3bc0f78af3b985bcbd2bb6b943 to your computer and use it in GitHub Desktop.
Python: Remove punctuation from string (quickly)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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