如果用编辑器写代码,很容易会怎么顺手怎么来,而每个人的写代码习惯有很大不同,这样会导致可读性不是很好,代码维护起来比较困难,当然各种编辑器一般都有各种用来自动排版的插件,例如Sublime Text就有:Python PEP8 Autoformat,使用方法:

默认快捷键:

Ctrl + Shift + R

不过,python的一些install脚本还是很强大的,例如python的autopep8库:
(链接:https://github.com/hhatto/autopep8)

$ pip install --upgrade autopep8

然后在命令行:

$ autopep8 --in-place --aggressive --aggressive <filename>

就可以对代码进行pep8自动排版,对比了下效果,如下:

排版前

import math, sys;def example1():####This is a long comment. This should be wrapped to fit within 72 characters.some_tuple=(   1,2, 3,'a'  );some_variable={'long':'Long code lines should be wrapped within 79 characters.','other':[math.pi, 100,200,300,9876543210,'This is a long string that goes on'],'more':{'inner':'This whole logical line should be wrapped.',some_tuple:[1,20,300,40000,500000000,60000000000000000]}}return (some_tuple, some_variable)
def example2(): return {'has_key() is deprecated':True}.has_key({'f':2}.has_key(''));
class Example3(   object ):def __init__    ( self, bar ):#Comments should have a space after the hash.if bar : bar+=1;  bar=bar* bar   ; return barelse:some_string = """Indentation in multiline strings should not be touched.
Only actual code should be reindented.
"""return (sys.path, some_string)

使用Sublime Text中的Python PEP8 Autoformat自动排版

import math
import sysdef example1():# This is a long comment. This should be wrapped to fit within 72# characters.some_tuple = (1, 2, 3, 'a')some_variable = {'long': 'Long code lines should be wrapped within 79 characters.','other': [math.pi, 100, 200, 300, 9876543210, 'This is a long string that goes on'],'more': {'inner': 'This whole logical line should be wrapped.', some_tuple: [1,20, 300, 40000, 500000000, 60000000000000000]}}return (some_tuple, some_variable)def example2(): return {'has_key() is deprecated': True}.has_key({'f': 2}.has_key(''))class Example3(object):def __init__(self, bar):# Comments should have a space after the hash.if bar:bar += 1bar = bar * barreturn barelse:some_string = """Indentation in multiline strings should not be touched.
Only actual code should be reindented.
"""return (sys.path, some_string)

使用autopep8自动排版

import math
import sysdef example1():# This is a long comment. This should be wrapped to fit within 72# characters.some_tuple = (1, 2, 3, 'a')some_variable = {'long': 'Long code lines should be wrapped within 79 characters.','other': [math.pi,100,200,300,9876543210,'This is a long string that goes on'],'more': {'inner': 'This whole logical line should be wrapped.',some_tuple: [1,20,300,40000,500000000,60000000000000000]}}return (some_tuple, some_variable)def example2(): return ('' in {'f': 2}) in {'has_key() is deprecated': True}class Example3(object):def __init__(self, bar):# Comments should have a space after the hash.if bar:bar += 1bar = bar * barreturn barelse:some_string = """Indentation in multiline strings should not be touched.
Only actual code should be reindented.
"""return (sys.path, some_string)

可以看到虽然都是PEP8风格,但是效果还是略有不同,可以根据需要个人习惯进行有选择的使用。

按PEP8风格自动排版Python代码相关推荐

  1. python black_如何使用Black自动格式化Python代码

    python black Writing Python code is one thing and writing the code in a good format is another thing ...

  2. 又一个Jupyter神器,操作Excel自动生成Python代码

    来源 | Python数据科学(ID: PyDataScience) 不得不说,Jupyter对于表的处理真的是越来越方便了,很多库可以直接实现可视化操作,无需写代码.但是这还不够,最近看到一个神器叫 ...

  3. 【Python】又一个Jupyter神器,操作Excel自动生成Python代码!

    大家好,我是东哥. 不得不说,Jupyter对于表的处理真的是越来越方便了,很多库可以直接实现可视化操作,无需写代码.但是这还不够,最近看到一个神器叫Mito,它真的是做到了无需写一行代码,而且手动的 ...

  4. 又一个 Jupyter 神器,操作 Excel 自动生成 Python 代码

    大家好,Jupyter对于表的处理真的是越来越方便了,很多库可以直接实现可视化操作,无需写代码. 但是这还不够,最近看到一个神器叫Mito,它真的是做到了无需写一行代码,而且手动的操作可以自动转换为代 ...

  5. vscode自动格式化python代码符合pep8

    安装格式化工具 打开命令行窗口安装以下工具 $ pip install -U flake8 $ pip install -U autopep8 在VScode配置中打开首选项–>设置,搜索pyt ...

  6. latex python_怎么在 LaTeX 中排版 Python 代码?

    用 LaTeX 排版代码的主要问题有两个.一是希望代码中的特殊字符不被 LaTeX 处理:二是希望能有代码高亮.前者称为「原样抄写」,也就是 verbatim.剩下的边框.编号.交叉引用,都属于 La ...

  7. python视频处理为卡通风格_使用python代码将照片变成卡通图片

    今次介绍用代码来实现这项任务,可以就此探查各种滤镜的内部机制. 制作环境:Windows10,Python2.7,Anaconda 任务描述:将D盘某文件夹中的所有图片使用代码进行卡通化,然后保存到另 ...

  8. Python代码自动排版工具(PEP8风格),autopep8

    autopep8是一款将python代码自动排版为的工具,autopep8安装 1.安装autopep8 pip install autopep8 2.PyCharm配置autopep8 Progra ...

  9. python代码风格

    pep8:Python代码风格检查工具 Python官网定义的代码风格 PEP 0008 – Style Guide for Python Code. pep8是检测编码风格是否符合 PEP 0008 ...

最新文章

  1. 洛谷 P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib
  2. c语言四则运算实验报告,c语言四则运算实验报告.doc
  3. Spring Boot 缓存应用实践
  4. 与Oracle配套组件,oracle 组件和体系结构
  5. spss如何选择需要的变量?
  6. java剪切txt文件_用Java把剪切板的内容实时保存到txt
  7. 随机数生成java代码_Java Bullshifier –生成大量随机代码库
  8. IP地址 子网掩码 网关
  9. win 7 ×××自动拨号设置
  10. 如何JQ将下拉列表的值传送给php,JQuery设置获取下拉菜单某个选项的值(比较全)...
  11. OpenGL基础5:第一个正方形
  12. GB2312-80 所有汉字排序-拼音
  13. 戒指戴在不同的手指上代表的意思
  14. The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals
  15. 服务器怎么预防常见的网络攻击
  16. 自己动手搭建苹果推送Push服务器
  17. 【UI设计】移动设备的界面设计尺寸
  18. 装配图中齿轮的画法_机械制图教程(8.2)装配图的规定画法和特殊画法
  19. 【吴恩达机器学习笔记】1引言、单变量线性回归、线性代数回顾
  20. Android13源码下载及全编译流程

热门文章

  1. ios运行html时黑屏,iOS-启动图黑屏解决方法
  2. 如何使用html实现在线秒表,请使用js实现一个秒表计时器的程序
  3. 玩客云通过openwrt作为旁路由
  4. 前端MUI+H5+HBuilderX开发APP(IOS,android),后台Springboot,java学习与实践文章,更新中(二)
  5. Babel的使用方法
  6. Qt官方示例-正则测试工具
  7. 免费生成微信小程序的经验
  8. 【读书分享】精益企业——高效能组织如何规模化创新?
  9. 盘点 | 从Uber的肇事无人车开始,细数这些年的人工智障
  10. csdn里的KaTex 公式语法