Created
March 11, 2011 19:35
-
-
Save myoffe/866435 to your computer and use it in GitHub Desktop.
Normalize whitespace in string
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
def normalize_whitespace(str): | |
import re | |
str = str.strip() | |
str = re.sub(r'\s+', ' ', str) | |
return str |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Trims whitespace from both sides and replaces all whitespace characters with a single space.
Example: " this is spacious ! " => "this is spacious !"