Converting spaces to python files?

I have code with formatted tab in python and I have some code in python format.

Integrating code into pain ... my editor wants to work in tabs or spaces, but not both.

Is there a command line function in linux or, well, anything that will convert Python code formatting anyway?

+6
python
source share
6 answers

reindent.py is a great script utility that converts any python.py file into one using indentation with 4 spaces and no tabs.

This is useful for "normalizing" code from disparate sources if you agree to agree to the four-dimensional standard. (Or, if you need tabs, you can run the reindent.py command, followed by the unix unexpand .)

PS. Your Python installation may have the reindent.py file installed in the Tools or Samples folder. On Ubuntu, it is provided by the python-examples package and is located in /usr/share/doc/python2.6/examples/Tools/scripts/reindent.py .

+3
source share

'man expand' for some info

he's at coreutils on Debian

+2
source share

You can use the expand and unpand Unix commands for this.

Usually, if I code in vim, for example, I automatically convert tabs to spaces.

my ~ / .vimrc looks something like this:

 set expandtab set tabstop=4 
+2
source share

Many editors (like vi) will convert tabs to or from spaces when you insert a line. Therefore, configure the tab options, but you want to postpone the whole file 1 step, and then undo one step, and then do it.

Vim Commands:

 1GVG <-- select entire file (i have this bound to CTRL-A) > <-- indent one step 1GVG <-- select again < <--- unindent one step 
+1
source share

What about Perl: perl -pe 's/(.*?)\t/$1.(" " x (4-length($1)%4))/ge' file_with_tabs.txt

python (this is from Markdown source ...):

 def _detab_sub(self, match): g1 = match.group(1) return g1 + (' ' * (self.tab_width - len(g1) % self.tab_width)) def _detab(self, text): if '\t' not in text: return text return self._detab_re.subn(self._detab_sub, text)[0] 
0
source share

A good programmer editor will have a team that converts tabs to spaces or vice versa / vice versa; You can also do this by searching and replacing in the editor.

0
source share

All Articles