How do I autoformat some Python code to be correctly formatted?(如何自动格式化一些 Python 代码以正确格式化?)
问题描述
我有一些现有的代码格式不一致——有时两个空格用于缩进,有时四个空格等等.代码本身是正确且经过充分测试的,但格式很糟糕.
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 代码以正确格式化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何自动格式化一些 Python 代码以正确格式化?
基础教程推荐
- 在 Python 中将货币解析为数字 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- Kivy 使用 opencv.调整图像大小 2022-01-01
