Expanding and Compressing Tabs
Credit: Alex Martelli
Problem
You want to convert tabs in a string to the appropriate number of spaces, or vice versa.
Solution
Changing tabs to the appropriate number of spaces is a reasonably
frequent task, easily accomplished with Python
strings’ built-in
expandtabs method. Because strings are immutable,
the method returns a new string object (a modified copy of the
original one). However, it’s easy to rebind a string
variable name from the original to the modified-copy value:
mystring = mystring.expandtabs( )
This doesn’t change the string object to which
mystring originally referred, but it does rebind
the name mystring to a newly created string object
in which tabs are expanded into runs of spaces.
Changing spaces into tabs is a rare and peculiar need. Compression, if that’s what you’re after, is far better performed in other ways, so Python doesn’t offer a built-in way to unexpand spaces into tabs. We can, of course, write one. String processing tends to be fastest in a split/process/rejoin approach, rather than with repeated overall string transformations:
def unexpand(astring, tablen=8):
import re
pieces = re.split(r'( +)', astring.expandtabs(tablen))
lensofar = 0
for i in range(len(pieces)):
thislen = len(pieces[i])
lensofar += thislen
if pieces[i][0]==' ':
numblanks = lensofar % tablen
numtabs = (thislen-numblanks+tablen-1)/tablen
pieces[i] = '\t'*numtabs + ' '*numblanks
return ''.join(pieces)Discussion
If expandtabs didn’t exist, we could write ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access