Austin
Austin

Reputation: 7319

How to replace custom tabs with spaces in a string, depend on the size of the tab?

I'm trying to write a python function not using any modules that will take a string that has tabs and replace the tabs with spaces appropriate for an inputted tabstop size. It can't just replace all size-n tabs by n spaces though, since a tab could be 1 to n spaces. I'm really confused, so if anyone could just point me in the right direction I'd greatly appreciate it.

For instance, if tabstop is size 4 originally:

123\t123 = 123 123 #one space in between

but changed to tabstop 5:

123\t123 = 123  123 #two spaces in between

I think I need to pad the end of the string with spaces until string%n==0 and then chunk it, but I'm pretty lost at the moment..

Upvotes: 12

Views: 22875

Answers (14)

user7711283
user7711283

Reputation:

Just only because this below does not fit into a comment to the answer of kzar who came up with a quite interesting approach (which does not answer the question because it uses a module), but didn't it correct :

import re
offsetAddon = 0
def spaces(tabSize=8):
  def replace(match):
    global offsetAddon
    spaceMultipl = (tabSize - (match.start(0) + offsetAddon) % tabSize)
    offsetAddon += (spaceMultipl - 1)
    return " " * spaceMultipl    
  return replace
tab=r'\t'
s="\t1\t12\t123\t1234\t12345\t123456\t1234567\t12345678\t12\t"
print(f'''"{re.sub(tab, spaces(4), s)}"''') # gives:  
# "    1   12  123 1234    12345   123456  1234567 12345678    12  "

Upvotes: 0

Waxrat
Waxrat

Reputation: 520

def expand_tabs(text: str, width: int = 8) -> str:
    """
    Expand each tab to one or more spaces
    """
    assert width > 0
    while (i := text.find('\t')) >= 0:
        text = text[:i] + ' ' * (width - i % width) + text[i+1:]
    return text

Upvotes: 0

Juande Manjon
Juande Manjon

Reputation: 11

Fix for @rémi answer This implementation honors the leading tab and any consecutive tabs

def replace_tab(s, tabstop=4):
    result = str()
    for c in s:
        if c == '\t':
            if (len(result) % tabstop == 0):
                result += ' ' * tabstop
            else:
                while (len(result) % tabstop != 0):
                    result += ' '
        else:
            result += c
    return result

Upvotes: 0

Vignesh A
Vignesh A

Reputation: 289

Here is the easiest way

def replaceTab(text,tabs)
    return text.replace('\t', ' ' * tabs)

Upvotes: 2

Ravi Bhanushali
Ravi Bhanushali

Reputation: 155

if you have the requirement where you want to add n spaces instead of custom tab you can simply write below code. I have shown the implementation using two functions, each having different way to solve it.You can use any of the function!

for eg. let the string be in the variable 'code' and 'x' be the size of tab

code = "def add(x, y)\f\treturn x + y"
x=4

def convertTabs(code, x):
    temp=""
    for i in range(0,x):
        temp+=" "
    return code.replace("\t",temp) 

def converTabs1(code,x):
    return code.replace("\t",x*" ")

both the functions above will give the same value, but the second one is super awesome !

Upvotes: 1

Felix
Felix

Reputation: 505

Use the re.sub is enough.

def untabify(s, tabstop = 4):
    return re.sub(re.compile(r'\t'), ' '*tabstop, s)

Upvotes: 0

andrea.m.piovesana
andrea.m.piovesana

Reputation: 41

I use .replace function that is very simple:

line = line.replace('\t', ' ')

Upvotes: 4

Samuel
Samuel

Reputation: 8905

I think Remi's answer is the simplest but it has a bug, it doesn't account for the case when you are already on a "tab stop" column. Tom Swirly pointed this out in the comments. Here's a tested fix to his suggestion:

def replace_tab(s, tabstop = 4):
    result = str()

    for c in s:
        if c == '\t':
            result += ' '
            while ((len(result) % tabstop) != 0):
                result += ' '
        else:
            result += c    

    return result

Upvotes: 2

kzar
kzar

Reputation: 3209

I needed something similar, here's what I came up with:

import re

def translate_tabs(tabstop = 8):
  offset = [0]
  def replace(match, offset=offset):
    offset[0] += match.start(0)
    return " " * (tabstop - offset[0] % tabstop)
  return replace

re.sub(r'\t', translate_tabs(4), "123\t123") 
# => '123 123'

re.sub(r'\t', translate_tabs(5), "123\t123")
# => '123  123'

Upvotes: 0

Jorge Antonio Galaz
Jorge Antonio Galaz

Reputation: 21

This programm replaces all the tabs for spaces in a file:

def tab_to_space (line, tab_lenght = 8):
    """this function change all the tabs ('\\t') for spaces in a string, 
        the lenght of the tabs is 8 by default"""

    while '\t' in line:
        first_tab_init_pos = line.find('\t')
        first_tab_end_pos = (((first_tab_init_pos // tab_lenght)+1) * tab_lenght)
        diff = first_tab_end_pos - first_tab_init_pos
        if diff == 0:
            spaces_string = ' ' * tab_lenght
        else:
            spaces_string = ' ' * diff
        line = line.replace('\t', spaces_string, 1)
    return line


inputfile = open('inputfile.txt', 'r')
outputfile = open('outputfile.txt', 'w')
for line in inputfile:
    line = tab_to_space(line)
    outputfile.write(line)
inputfile.close()
outputfile.close()

Upvotes: 1

Joel Cornett
Joel Cornett

Reputation: 24788

For a tab length of 5:

>>> s = "123\t123"
>>> print ''.join('%-5s' % item for item in s.split('\t'))
123  123  
>>> 

Upvotes: 5

emigue
emigue

Reputation: 502

This code can help you:

initial_string = "My \tstring \ttest\t"
block_size = "5"
"".join([("{block_value:"+str(block_size)+"s}").format(block_value=block) 
    for block in initial_string.split("\t")])

You will need to study: format, split and join function and list comprehension concept.

Upvotes: 1

Rémi
Rémi

Reputation: 527

Since you wan't a python function that doesn't use any external module, I think you should design first the algorithm of your function...

I would propose to iterate on every char of the string ; if char i is a tab, you need to compute how many spaces to insert : the next "aligned" index is ((i / tabstop) + 1) * tabstop. So you need to insert ((i / tabstop) + 1) * tabstop - (i % tabstop). But an easier way is to insert tabs until you are aligned (i.e. i % tabstop == 0)

def replace_tab(s, tabstop = 4):
  result = str()
  for c in s:
    if c == '\t':
      while (len(result) % tabstop != 0):
        result += ' ';
    else:
      result += c    
  return result

Upvotes: 4

ibi0tux
ibi0tux

Reputation: 2619

Sorry, i misread the question the first time.

This is a recursive version that should work for any number of tabs in the input :

def tabstop ( s , tabnum = 4):
    if not '\t' in s:
        return s
    l = s.find('\t')
    return s[0:l]+' '*(tabnum-l)+tabstop(s[l+1:],tabnum)

Upvotes: 2

Related Questions