space/tab conversion utility?

Tim Peters tim.peters at
Thu Jul 29 03:51:52 CEST 2004

[Grant Edwards, wants to convert spaces to tabs]
> ...
> 2) Simply converting all leading spaces to the right number of
>   tabs (unexpand knows how to do that) isn't correct.  Only
>   the spaces that are block-indent spaces should be converted.
... is in your Python distribution, and is the state of the
art for "intelligent" conversion of tab-infected files to
space-celebrating ones.  I understand that's not the direction you
want, but it is the *code* you want to start from.  Doing a good job
on this is harder than anyone believes until they've failed at least
once, and the problem is indeed dealing with "semantically
insignifcant" whitespace in a pleasant way, trying to guess the
author's intent about visual appearance. is aware of the
*semantic* indentation level of each line, and you could fiddle its
internals to convert just the semantically significant leading spaces
to hard tabs.

More information about the Python-list mailing list