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 代码以正确格式化?
- 使用Heroku上托管的Selenium登录Instagram时,找不到元素';用户名'; 2022-01-01
- 如何在 Python 的元组列表中对每个元组中的第一个值求和? 2022-01-01
- python-m http.server 443--使用SSL? 2022-01-01
- 如何在 python3 中将 OrderedDict 转换为常规字典 2022-01-01
- 沿轴计算直方图 2022-01-01
- python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令 2022-01-01
- pytorch 中的自适应池是如何工作的? 2022-07-12
- padding='same' 转换为 PyTorch padding=# 2022-01-01
- 如何将一个类的函数分成多个文件? 2022-01-01
- 分析异常:路径不存在:dbfs:/databricks/python/lib/python3.7/site-packages/sampleFolder/data; 2022-01-01
