views:

7703

answers:

5

It seems like there should be a simpler way than:

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

Is there?

+4  A: 

Not necessarily simpler, but a different way, if you are more familiar with the re family.

import re, string
s = "string. With. Punctuation?" # Sample string 
out = re.sub('[%s]' % re.escape(string.punctuation), '', s)
Vinko Vrsalovic
Works because string.punctuation has the sequence ,-. in proper, ascending, no-gaps, ASCII order. While Python has this right, when you try to use a subset of string.punctuation, it can be a show-stopper because of the surprise "-".
S.Lott
Actually, its still wrong. The sequence "\]" gets treated as an escape (coincidentally not closing the ] so bypassing another failure), but leaves \ unescaped. You should use re.escape(string.punctuation) to prevent this.
Brian
Yes, I omitted it because it worked for the example to keep things simple, but you are right that it should be incorporated.
Vinko Vrsalovic
A: 

Do search and replace using the regex functions, as seen here.. If you have to repeatedly perform the operation, you can keep a compiled copy of the regex pattern (your punctuation) around, which will speed things up a bit.

Dana the Sane
Is string.punctuation locale corrected? If so, this might not be the best solution.
EBGreen
I'm not sure, I haven't used it. I'm assuming that the poster/reader will know what punctuation they are replacing.
Dana the Sane
Ehh...I don't know either. I would expect .punctuation to be locale corrected, but I wouldn't rely on it. You are probably right that if the user has a specific set of characters, then a compiled regex would be a good way to go.
EBGreen
+3  A: 

I usually use something like this:

>>> s = "string. With. Punctuation?" # Sample string
>>> import string
>>> for c in string.punctuation:
...     s= s.replace(c,"")
...
>>> s
'string With Punctuation'
S.Lott
+28  A: 

From an efficiency perspective, you're not joing to beat translate() - it's performing raw string operations in C with a lookup table - there's not much that will beat that bar writing your own C code. If speed isn't a worry, another option though is:

exclude = set(string.punctuation)
s = ''.join(ch for ch in s if ch not in exclude)

This is faster than s.replace with each char, but won't perform as well as non-pure python approaches such as regexes or string.translate, as you can see from the below timings. For this type of problem, doing it at as low a level as possible pays off.

Timing code:

import re, string, timeit

s = "string. With. Punctuation"
exclude = set(string.punctuation)
table = string.maketrans("","")
regex = re.compile('[%s]' % re.escape(string.punctuation))

def test_set(s):
    return ''.join(ch for ch in s if ch not in exclude)

def test_re(s):  # From Vinko's solution, with fix.
    return regex.sub('', s)

def test_trans(s):
    return s.translate(table, string.punctuation)

def test_repl(s):  # From S.Lott's solution
    for c in string.punctuation:
        s=s.replace(c,"")
    return s

print "sets      :",timeit.Timer('f(s)', 'from __main__ import s,test_set as f').timeit(1000000)
print "regex     :",timeit.Timer('f(s)', 'from __main__ import s,test_re as f').timeit(1000000)
print "translate :",timeit.Timer('f(s)', 'from __main__ import s,test_trans as f').timeit(1000000)
print "replace   :",timeit.Timer('f(s)', 'from __main__ import s,test_repl as f').timeit(1000000)

This gives the following results:

sets      : 19.8566138744
regex     : 6.86155414581
translate : 2.12455511093
replace   : 28.4436721802
Brian
Thanks for the timing info, I was thinking about doing something like that myself, but yours is better written than anything I would have done and now I can use it as a template for any future timing code I want to write:).
Lawrence Johnston
Great answer, thanks.
Ali A
+2  A: 
myString.translate(None, string.punctuation)
pyrou
ah, I tried this but it doesn't work in all cases. myString.translate(string.maketrans("",""), string.punctuation) works fine.
Aidan Kane