python 格式化程序autopep8,粘贴格式VSCode

问题描述

我想复制粘贴一些代码,但是代码的格式太脏了。

例如

n = input()

while True:
    try:
        if (int(n) < 10):
            pass
        else:
            pass
    except:
        break

粘贴后

n = input()

while True:
    try:
        if (int(n) < 10):
            output = str(n) * 2
        else:
            pass
        
        if (int(n) < 10):
    pass
else:
    pass

    except:
        break

我想要

n = input()

while True:
    try:
        if (int(n) < 10):
            pass
        else:
            pass

        if (int(n) < 10):
            pass
        else:
            pass

    except:
        break

第二个也不能像 autopep8 这样的格式化程序工作。 我的意思是 autopep8 可以把第二个改成第三个。

正常吗? 甚至选项是 "editor.formatOnSave": 真,"editor.formatOnPaste": true

请帮帮我。谢谢你。 祝你有美好的一天:)

解决方法

我爱上了 https://pypi.org/project/black/,它会在保存之前自动在编辑器中格式化您的代码。安装简单,无需维护。只需查看如何将其安装到您的编辑器即可。