def remove_whitespaces(value):
"Remove all whitespaces"
p = re.compile(r'\s+')
return p.sub(' ', value)
The above code strips tags but doesn’t remove “all” whitespaces from the value.
Thanks
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The fastest general approach eschews REs in favor of string’s fast, powerful
.translatemethod:In 2.6, it’s even simpler, just
Note that this applies to “plain” Python 2.* strings, i.e., bytestrings — Unicode’s strings’
.translatemethod is somewhat different — it takes a single argument which must be a mapping ofordvalues for Unicode characters to Unicode strings, orNonefor deletion. I.e., taking advantage ofdict‘s handy.fromkeysclassmethod:to remove exactly the same set of characters. Of course, Unicode also has more characters you could consider whitespace and want to remove — so you’d probably want to build a mapping
unicode_nospacebased on information from module unicodedata, rather than using this simpler approach based on module string.