Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: How to convert tabs to spaces? [duplicate]

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?

like image 912
user553702 Avatar asked Aug 30 '26 10:08

user553702


1 Answers

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)

like image 56
Colleen Avatar answered Sep 01 '26 00:09

Colleen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!