textwrap.dedent replaces tabs?

Tom Plunket tomas at fancy.org
Tue Dec 19 01:36:15 EST 2006


Peter Otten wrote:

> > I guess I could manually replace all tabs with eight
> > spaces (as opposed to 'correct' tab stops), and then replace them when
> > done, but it's probably just as easy to write a non-destructive dedent.
> 
> You mean, as easy as
> 
> >>> "\talpha\tbeta\t".expandtabs()
> '        alpha   beta    '
> 
> ?

Umm, no, that's why I wrote "eight spaces (as opposed to 'correct' tab
stops)".

In either case, though, it'd be hard to know how to replace the tabs, so
it'd be better not to remove them in the first place.  Indeed, dedent()
would work perfectly for my cases if it simply didn't do the
expandtabs() in the first place, but there'd be some pretty glaring
holes in its logic then.

I've since written up a fairly reasonable (IMO) replacement though, that
started with textwrap.dedent(), removed the expandtabs() call, and 
Does The Right Thing with tabs vs. spaces (e.g. it doesn't treat a tab
at the beginning of a line as the same as eight spaces).


-tom!

-- 



More information about the Python-list mailing list