擅长:python、mysql、java
<p>如果要删除多个空白项并用单个空格替换它们,最简单的方法是使用这样的regexp:</p>
<pre><code>>>> import re
>>> myString="I want to Remove all white \t spaces, new lines \n and tabs \t"
>>> re.sub('\s+',' ',myString)
'I want to Remove all white spaces, new lines and tabs '
</code></pre>
<p>如果愿意,可以用<code>.strip()</code>删除尾随空格。</p>