<p>我有一个表格形式的命令输出。我正在分析结果文件的输出并将其存储在字符串中。一行中的每个元素都由一个或多个空格字符分隔,因此我使用正则表达式来匹配一个或多个空格并将其拆分。但是,在每个元素之间插入一个空格:</p>
<pre><code>>>> str1="a b c d" # spaces are irregular
>>> str1
'a b c d'
>>> str2=re.split("( )+", str1)
>>> str2
['a', ' ', 'b', ' ', 'c', ' ', 'd'] # 1 space element between!!!
</code></pre>
<p>有更好的办法吗?</p>
<p>每次拆分后<code>str2</code>都会附加到列表中。</p>