我可以在python中隐式字符串加入时得到lint错误吗?

Mob*_*erg 10 python

有没有办法在文字字符串列表中的错过逗号上获取lint错误?

例:

exceptions = ["banana", "pineapple", "apple"
              "pen"]
Run Code Online (Sandbox Code Playgroud)

您可能认为此列表包含4个项目,但事实是要说明!"apple"和"pen"加入"applepen".

我害怕这些省略的逗号.有没有一些lint工具可以帮我找到它们?

例2:

exceptions = ["Carmichael",
              "Vanessa"      # <--- Spot the missing comma
              "Ford"]
Run Code Online (Sandbox Code Playgroud)

Jos*_*mez -1

SublimeText3 带有插件 flake8,它是其他 python lint 插件的包装器,可以修复它。

否则,您可以编写一个脚本来计算一行中的 ((")/2)-1 和逗号,如果结果不匹配,则添加逗号。

编辑:

我所说的解释:

    def countQuotes(string):
        return string.count('"')
    def countCommas(string):
        return string.count(',')
    files = os.listdir('your/directory')
    for filename in files:
    if filename.endswith(".py"):
        with fileinput.FileInput("your/directory"+"/"+filename, inplace=True, backup='.bak') as fileContent:
        for line in fileContent:
            if '"' in line:
                numQuotes = countQuotes(line)
                numCommas = countCommas(line)
                if(numQuotes == 2 and ']' in line):
                    if(numCommas != 0):
                        #error, must delete a comma in right place and print line
                    else:
                        print(line)
                if(numQuotes == 2 and ']' not in line):
                    if(numCommas != 1):
                        #error, must add a comma in right place and print line
                    else:
                        print(line)
                if(numQuotes > 2):
                    if(numCommas > (numQuotes//2)-1)
                        #error, must delete a comma in right place and print line
                    elif(numCommas < (numQuotes//2)-1)
                        #error, must add a comma in right place and print line
                    else:
                        print(line)
           else:
                print(line)
Run Code Online (Sandbox Code Playgroud)

这个方法一定有效,只要想想你必须在哪里插入或删除逗号才能最终得到你想要的格式。