python - Regex for removing whitespace -
def remove_whitespaces(value): "remove whitespaces" p = re.compile(r'\s+') return p.sub(' ', value) the above code strips tags doesn't remove "all" whitespaces value.
thanks
the fastest general approach eschews res in favor of string's fast, powerful .translate method:
import string identity = string.maketrans('', '') def remove_whitespace(value): return value.translate(identity, string.whitespace) in 2.6, it's simpler, just
return value.translate(none, string.whitespace) note applies "plain" python 2.* strings, i.e., bytestrings -- unicode's strings' .translate method different -- takes single argument must mapping of ord values unicode characters unicode strings, or none deletion. i.e., taking advantage of dict's handy .fromkeys classmethod:
nospace = dict.fromkeys(ord(c) c in string.whitespace) def unicode_remove_whitespace(value): return value.translate(nospace) to remove same set of characters. of course, unicode has more characters consider whitespace , want remove -- you'd want build mapping unicode_nospace based on information module unicodedata, rather using simpler approach based on module string.
Comments
Post a Comment