[Tutor] How o convert spaces into tabs??
mwalsh at mwalsh.org
Tue Jun 2 21:53:36 CEST 2009
vince spicer wrote:
> regex will do it
> import re
> line = re.sub(r"\s+", "\t", line)
> print line
The above replaces the newline, which reminds me that even seemingly
trivial uses of 're' can become not-so-trivial in a hurry.
In : import re
In : line = '1 2 3 4 5\n'
In : re.sub('\s+', '\t', line)
Maybe this is closer to your intent, but I refuse to guarantee it ;)
Better to stick with str methods whenever possible.
In : re.sub('[ ]+', '\t', line)
More information about the Tutor