textwrap.dedent replaces tabs?

Tom Plunket tomas at fancy.org
Sun Dec 17 01:45:35 EST 2006


CakeProphet wrote:

> Hmmm... a quick fix might be to temporarily replace all tab characters
> with another, relatively unused control character.
> 
> MyString = MyString.replace("\t", chr(1))
> MyString = textwrap.dedent(MyString)
> MyString = MyString.replace(chr(1), "\t")
> 
> Of course... this isn't exactly safe, but it's not going to be fatal,
> if it does mess something up. As long as you don't expect receiving any
> ASCII 1 characters.

Well, there is that small problem that there are leading tabs that I
want stripped.  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.

It's not that I don't understand /why/ it does it; indeed I'm sure it
does this so you can mix tabs and spaces in Python source.  Why anyone
would intentionally do that, though, I'm not sure.  ;)

-tom!

-- 



More information about the Python-list mailing list