3

Possible Duplicate:
Enforce “spaces” or “tabs” only in python files?

I got Python code that has mixed tabs and spaces and is very difficult to read or understand the indentation, because lines look like they are at a certain indentation in the IDE but Python parses them as a different indentation than what they look like. Do tabs in Python count for a certain hard-coded number of spaces? Is there a way to canonicalize a Python script that has mixed tabs/spaces, to use consistent spacing?

Community
  • 1
  • 1
user553702
  • 2,819
  • 5
  • 23
  • 27
  • 1
    Look at this: http://stackoverflow.com/questions/338767/tool-to-convert-python-indentation-from-spaces-to-tabs?rq=1 – Marcin Dec 14 '12 at 22:33

1 Answers1

1

There is no canonical value for the number of spaces that = 1 tab in python (I like 4, but that's just me).

What you can do is read the file in and search for \t characters, and replace those with however many spaces you need.

EDIT: Something that will probably be useful to you in the future is Python's style guide (aka PEP8)

Colleen
  • 23,899
  • 12
  • 45
  • 75
  • 2
    I don't think this comment or the other linked answer or the PEP really answer the question... Sometimes a situation happens where someone (let's say Alice) sends someone else (Bob) a Python module replete with mixed spaces and tabs. Alice used an IDE that accidentally causes them to be mixed, and then forgot what that IDE was. The file executes just fine with the Python interpreter, but Bob's text editor / IDE cannot display the code in a sensible way. How do we canonicalize the file to an equivalent one with only space indentation that executes equally? – user553702 Sep 25 '15 at 18:03
  • Actually, in that context the 'canonical' form of the file is the pep8 form. – cbz Jun 20 '18 at 15:22