Script that converts between indentation and curly braces in Python code
Chris Angelico
rosuav at gmail.com
Wed Jul 31 10:23:54 EDT 2013
On Wed, Jul 31, 2013 at 3:07 PM, Rotwang <sg552 at hotmail.co.uk> wrote:
>> # Assumes spaces OR tabs but not both
>> # Can't see an easy way to count leading spaces other than:
>> # len(s)-len(s.lstrip())
>
>
> How about len(s.expandtabs()) - len(s.lstrip()) instead?
Still comes to the same thing. The only diff is that tabs get treated
as eight spaces instead of one (and the bug that a tab elsewhere in
the line will result in indentation, which is fixed by lstripping the
tab-expanded form). It won't perfectly round-trip with a mixture of
tabs and spaces; as it is, you can pick one or the other and run with
it. Anyway, the main point is that indentation will work. Sure you
might have ugly narrow code, but it'll run with one-space indents.
ChrisA
More information about the Python-list
mailing list