【发布时间】:2011-02-28 00:08:43
【问题描述】:
可能重复:
Substitute multiple whitespace with single whitespace in Python
试图弄清楚如何编写一个给定字符串的正则表达式:
"hi this is a test"
我可以把它变成
"hi this is a test"
空白被归一化为一个空格
有什么想法吗?非常感谢
【问题讨论】:
可能重复:
Substitute multiple whitespace with single whitespace in Python
试图弄清楚如何编写一个给定字符串的正则表达式:
"hi this is a test"
我可以把它变成
"hi this is a test"
空白被归一化为一个空格
有什么想法吗?非常感谢
【问题讨论】:
import re
re.sub("\s+"," ",string)
【讨论】:
它需要是一个正则表达式吗?
我只是用
new_string = " ".join(re.split(s'\s+', old_string.strip()))
【讨论】:
sed
sed 's/[ ]\{2,\}/ /g'
【讨论】: