def collapse_tabs(string, indentation=False, replace=' '):
 p = []
 for x in string.splitlines():
  n = ((indentation and (len(x) - len(x.lstrip()))) or 0)
  p.append((x[:n] + RE_TABS.sub(replace, x[n:]).strip()))
 return '\n'.join(p)

def collapse_tabs ( string , indentation = False , replace = ' ' ) : p = [ ] for x in string . splitlines ( ) : n = ( ( indentation and ( len ( x ) - len ( x . lstrip ( ) ) ) ) or 0 ) p . append ( ( x [ : n ] + RE_TABS . sub ( replace , x [ n : ] ) . strip ( ) ) ) return '\n' . join ( p )
returns a string with tabs replaced by a single space .