如何修复Python缩进

时间:2009-06-21 18:10:18

标签: python

我有一些Python代码有不一致的缩进。有很多标签和空格的混合使事情变得更糟,甚至空间压痕也不会保留。

代码按预期工作,但很难维护。

如何在不破坏代码的情况下修复缩进(如HTML Tidy,但对于Python)?

14 个答案:

答案 0 :(得分:271)

使用您在Python安装的reindent.py目录中找到的Tools/scripts/脚本:

  

更改要使用的Python(.py)文件   4空间缩进,没有硬标签   字符。还要修剪多余的空间   和行的末尾的标签,和   删除末尾的空行   文件。还要确保最后一行结束   换行。

查看该脚本以获取详细的使用说明。

答案 1 :(得分:55)

如果您使用的是Vim,请参阅:h retab

                                                        *:ret* *:retab*
:[range]ret[ab][!] [new_tabstop]
                        Replace all sequences of white-space containing a
                        <Tab> with new strings of white-space using the new
                        tabstop value given.  If you do not specify a new
                        tabstop size or it is zero, Vim uses the current value
                        of 'tabstop'.
                        The current value of 'tabstop' is always used to
                        compute the width of existing tabs.
                        With !, Vim also replaces strings of only normal
                        spaces with tabs where appropriate.
                        With 'expandtab' on, Vim replaces all tabs with the
                        appropriate number of spaces.
                        This command sets 'tabstop' to the new value given,
                        and if performed on the whole file, which is default,
                        should not make any visible change.
                        Careful: This command modifies any <Tab> characters
                        inside of strings in a C program.  Use "\t" to avoid
                        this (that's a good habit anyway).
                        ":retab!" may also change a sequence of spaces by
                        <Tab> characters, which can mess up a printf().
                        {not in Vi}
                        Not available when |+ex_extra| feature was disabled at
                        compile time.

例如,如果您只需输入

:ret

所有标签都会展开到空格中。

你可能想要

:se et  " shorthand for :set expandtab

确保所有新行都不使用文字标签。


如果您不使用Vim,

perl -i.bak -pe "s/\t/' 'x(8-pos()%8)/eg" file.py

将使用空格替换制表符,假设制表符在file.py中每8个字符停止一次(原始值为file.py.bak,以防万一)。如果您的制表符每隔4个空格,则将4s替换为4s。

答案 2 :(得分:42)

我会联系autopep8来执行此操作:

$ # see what changes it would make
$ autopep8 path/to/file.py --select=E101,E121 --diff

$ # make these changes
$ autopep8 path/to/file.py --select=E101,E121 --in-place

注意:E101 and E121是pep8缩进(我认为您只需通过--select=E1来修复所有与缩进相关的问题 - 以E1开头的那些问题。

您可以使用递归标记将其应用于整个项目:

$ autopep8 package_dir --recursive --select=E101,E121 --in-place

另请参阅Tool to convert Python code to be PEP8 compliant

答案 3 :(得分:26)

autopep8 -i script.py

使用autopep8

autopep8 自动格式化Python代码以符合PEP 8 nullstyle指南。它使用pep8实用程序来确定哪些部分  nullcode需要格式化。 autopep8能够修复大部分内容  nullformatting可以报告的pep8个问题。

pip install autopep8
autopep8 script.py    #  print only
autopep8 -i script.py # write file

答案 4 :(得分:10)

使用Vim,它不应该比点击 Esc ,然后输入...

更多参与
:%s/\t/    /g

...在您要更改的文件上。这会将所有标签转换为四个空格。如果你的间距也不一致,那就更难了。

答案 5 :(得分:8)

还有PythonTidy(因为你说你喜欢HTML Tidy)。

它可以做的不仅仅是清理标签。如果您喜欢这种类型的东西,那值得一看。

答案 6 :(得分:5)

如果你很懒(像我一样),你也可以下载一个试用版的Wingware Python IDE,它有一个自动修复工具,用于搞乱缩进。它工作得很好。 http://www.wingware.com/

答案 7 :(得分:5)

在大多数类UNIX系统上,您还可以运行:

expand -t4 oldfilename.py > newfilename.py

从命令行,如果要用4以外的空格替换制表符,则更改数字。您可以轻松编写shell脚本,一次性使用一堆文件,保留原始文件名。

答案 8 :(得分:3)

由于缺少一些模块,reindent脚本对我不起作用。无论如何,我找到了这个sed命令,它完美地完成了我的工作:

sed -r 's/^([  ]*)([^ ])/\1\1\2/' file.py

答案 9 :(得分:2)

试试Emacs。它对Python中所需的缩进有很好的支持。请查看此链接http://python.about.com/b/2007/09/24/emacs-tips-for-python-programmers.htm

答案 10 :(得分:0)

我有一个解决这个问题的简单方法。你可以先输入“:retab”然后再输入“:retab!”,然后一切都会好的

答案 11 :(得分:0)

尝试IDLE,然后使用 Alt + X 查找缩进。

答案 12 :(得分:0)

如果尝试寻找将2-space indented python脚本转换为tab indented版本的工具,只需使用此在线工具即可:

https://www.tutorialspoint.com/online_python_formatter.htm

答案 13 :(得分:0)

还有一个名为 YAPF

的 Google 很棒的项目

它可以自动重新格式化整个项目或检查项目是否有正确的缩进/格式。我在一个商业项目中使用了它,我建议这样做。