我有一些现有的代码格式不一致——有时两个空格用于缩进,有时四个空格等等.代码本身是正确且经过充分测试的,但格式很糟糕.
I have some existing code which isn't formatted consistently -- sometimes two spaces are used for indent, sometimes four, and so on. The code itself is correct and well-tested, but the formatting is awful.
网上有没有一个地方可以让我简单地粘贴一段 Python 代码并自动为我缩进/格式化?或者,是否有一个 X 以便我可以执行类似 X --input=*.py 之类的操作并让它用格式化版本覆盖每个文件?
Is there a place online where I can simply paste a snippet of Python code and have it be indented/formatted automatically for me? Alternatively, is there an X such that I can do something like X --input=*.py and have it overwrite each file with a formatted version?
现在,我会推荐 autopep8,因为它不仅可以纠正缩进问题,而且(由您自行决定)使代码符合许多其他 PEP8 准则.
Nowadays, I would recommend autopep8, since it not only corrects indentation problems but also (at your discretion) makes code conform to many other PEP8 guidelines.
使用 reindent.py.它应该与 Python 的标准发行版一起提供,但在 Ubuntu 上您需要安装 python2.6-examples 包.
Use reindent.py. It should come with the standard distribution of Python, though on Ubuntu you need to install the python2.6-examples package.
您也可以在 网络上找到它.
You can also find it on the web.
此脚本尝试将任何 python 脚本转换为符合 4-space 标准.
This script attempts to convert any python script to conform with the 4-space standard.
这篇关于如何自动格式化一些 Python 代码以正确格式化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
如何在 Python 中将货币字符串转换为浮点数?How do I convert a currency string to a floating point number in Python?(如何在 Python 中将货币字符串转换为浮点数?)
在 Pandas 中解析多索引 Excel 文件Parsing a Multi-Index Excel File in Pandas(在 Pandas 中解析多索引 Excel 文件)
pandas 时间序列 between_datetime 函数?pandas timeseries between_datetime function?( pandas 时间序列 between_datetime 函数?)
pandas 重新采样到每月的特定工作日pandas resample to specific weekday in month( pandas 重新采样到每月的特定工作日)
在 Pandas 中合并/组合两个具有不同频率时间序列Merging/combining two dataframes with different frequency time series indexes in Pandas?(在 Pandas 中合并/组合两个具有不同频率时间序列索
Python - 如何标准化时间序列数据Python - how to normalize time-series data(Python - 如何标准化时间序列数据)