>>> import re
>>> text = "JGood is a handsome boy, he is cool, clever, and so on..."
>>> print(re.sub(r'\s+', '-', text))
JGood-is-a-handsome-boy,-he-is-cool,-clever,-and-so-on...
>>> print(re.sub(r'is\s+', '-', text))
JGood -a handsome boy, he -cool, clever, and so on...
>>> print(re.sub(r'\s+\.', '.', text))
JGood is a handsome boy, he is cool, clever, and so on...
>>> text = "JGood is a handsome boy , he is cool , clever , and so on..."
>>> print(re.sub(r'\s+,\s+', ',',text))
JGood is a handsome boy,he is cool,clever,and so on...
>>>
re.sub
re.sub用于替换字符串中的匹配项。
import re
text = "JGood is a handsome boy, he is cool, clever, and so on..."
print re.sub(r'\s+', '-', text)
#将空格替换成—,\s代表空格。