Sometimes it takes more time to figure out a regular expression than just writing it in python:
import string s = "how much for the maple syrup? $20.99? That ricidulous!!!" for char in string.punctuation: s = s.replace(char, ' ')
If you need other characters, you can change it to use the whitelist or expand your blacklist.
Sample whitelist:
whitelist = string.letters + string.digits + ' ' new_s = '' for char in s: if char in whitelist: new_s += char else: new_s += ' '
An example of a whitelist using a generator expression:
whitelist = string.letters + string.digits + ' ' new_s = ''.join(c for c in s if c in whitelist)
monkut May 18 '09 at 9:24 a.m. 2009-05-18 09:24
source share