我有一些 Python 代码的缩进不一致。大量制表符和空格的混合使情况变得更糟,甚至空格缩进也没有保留。
该代码按预期工作,但难以维护。
我怎样才能修复缩进(比如HTML 整洁 https://en.wikipedia.org/wiki/HTML_Tidy,但对于 Python)而不破坏代码?
Some editor-specific advice:
-
Vi/Vim:
- Replace tabs with spaces in vim https://stackoverflow.com/questions/426963
- Expand tabs to spaces in vim only in python files? https://stackoverflow.com/questions/9986475
- Use Vim Retab to solve TabError: inconsistent use of tabs and spaces in indentation? https://stackoverflow.com/questions/48735671
-
Notepad++:
- Convert tabs to spaces in Notepad++ https://stackoverflow.com/questions/455037
- Auto-indent in Notepad++ https://stackoverflow.com/questions/412427
-
Emacs:
- Indentation not working properly in emacs for python https://stackoverflow.com/questions/27032218
Use the reindent.py
您在中找到的脚本Tools/scripts/
Python 安装目录:
更改要使用的 Python (.py) 文件
4 个空格缩进且无硬制表符
人物。还要修剪多余的空间
和行尾的制表符,以及
删除末尾的空行
文件。还要确保最后一行结束
带换行符。
查看该脚本以获取详细的使用说明。
注意:如果您的 Linux 发行版默认情况下没有使用 Python 安装重新缩进:
许多 Linux 发行版没有reindent
默认安装python
--> 一种简单的获取方法reindent
就是要做pip install reindent
.
附注替代方案pip
是使用您的发行版包管理器(即apt-get
, yum
, dnf
)但随后您需要弄清楚哪个包具有命令行工具,因为每个发行版都在不同的包中包含该工具。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)