簡體   English   中英

如何修復 Python 縮進

[英]How to fix Python indentation

我有一些具有不一致縮進的 Python 代碼。 有很多制表符和空格的混合使事情變得更糟,甚至空格縮進也沒有保留。

代碼按預期工作,但很難維護。

如何在不破壞代碼的情況下修復縮進(如HTML Tidy ,但適用於 Python)?

使用在 Python 安裝的Tools/scripts/目錄中找到的reindent.py腳本:

更改 Python (.py) 文件以使用 4 個空格縮進並且沒有硬制表符。 還要修剪行尾多余的空格和制表符,並刪除文件末尾的空行。 還要確保最后一行以換行符結尾。

查看該腳本以獲取詳細的使用說明。


注意:如果你的 Linux 發行版沒有默認安裝 Python 的 reindent:

許多 linux 發行版默認沒有使用python安裝reindent --> 獲得reindent一種簡單方法是執行pip install reindent

ps pip的替代方法是使用您的發行版包管理器(即apt-getyumdnf ),但是您需要弄清楚哪個包具有命令行工具,因為每個發行版在不同的包中都有該工具。

我會使用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 和 E121是 pep8 縮進(我認為您可以簡單地傳遞--select=E1來修復所有與縮進相關的問題 - 那些以 E1 開頭的問題)。

您可以使用遞歸標志將其應用於整個項目:

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

另請參閱將 Python 代碼轉換為符合 PEP8 的工具

如果您使用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 (原始文件轉到file.py.bak ,以防萬一)。 如果您的制表位是每 4 個空格,則將 8s 替換為 4s。

autopep8 -i script.py

使用autopep8

autopep8自動格式化 Python 代碼以符合PEP 8 nullstyle指南。 它使用pep8實用程序來確定需要格式化nullcode哪些部分。 autopep8能夠修復大多數可由pep8報告的pep8 nullformatting問題。

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

使用 Vim,它不應該比點擊Esc更復雜,然后輸入...

:%s/\t/    /g

...在您要更改的文件上。 這會將所有制表符轉換為四個空格。 如果您的間距也不一致,那將更加困難。

還有PythonTidy (因為你說你喜歡 HTML Tidy)。

不過,它可以做的不僅僅是清理標簽。 如果你喜歡這種類型的東西,那么值得一看。

在大多數類 UNIX 系統上,您還可以運行:

expand -t4 oldfilename.py > newfilename.py

從命令行,如果您想用 4 以外的多個空格替換制表符,請更改數字。您可以輕松編寫一個 shell 腳本來同時處理一堆文件,同時保留原始文件名。

由於缺少一些模塊,reindent 腳本對我不起作用。 無論如何,我發現這個sed命令非常適合我:

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

如果你很懶(像我一樣),你也可以下載 Wingware Python IDE 的試用版,它有一個自動修復工具來處理混亂的縮進。 它工作得很好。 http://www.wingware.com/

試試 Emacs。 它對 Python 中所需的縮進有很好的支持。 請檢查此鏈接http://python.about.com/b/2007/09/24/emacs-tips-for-python-programmers.htm

嘗試IDLE ,並使用Alt + X查找縮進。

對於這個問題,我有一個簡單的解決方案。 你可以先輸入":retab"然后輸入":retab!",然后一切都會好的

如果試圖找到將2-space indented Python 腳本制作為tab indented版本的工具,只需使用此在線工具:

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

還有一個叫YAPF的 Google 很棒的項目

它可以自動重新格式化整個項目或檢查項目是否有正確的縮進/格式。 我在一個商業項目中使用了它,我推薦它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM