Python、PEP-8、E122续行缺少缩进或凹陷

Python、PEP-8、E122续行缺少缩进或凹陷,python,pep8,Python,Pep8,我得到这个错误,但无论我选择缩进它,我仍然得到它,你知道为什么吗 if len(argmaxcomp) == 1: print "The complex with the greatest mean abundance is: {0}"\ .format(argmaxcomp[0]) 有一条是这样的: 包装长行的首选方法是在括号、方括号和大括号内使用Python的隐含行继续。通过将表达式括在括号中,可以在多行上打断长行。应该优先使用这些选项,而不是使用反斜杠作为行延续 有时反斜

我得到这个错误,但无论我选择缩进它,我仍然得到它,你知道为什么吗

if len(argmaxcomp) == 1:
    print "The complex with the greatest mean abundance is: {0}"\
    .format(argmaxcomp[0])
有一条是这样的:

包装长行的首选方法是在括号、方括号和大括号内使用Python的隐含行继续。通过将表达式括在括号中,可以在多行上打断长行。应该优先使用这些选项,而不是使用反斜杠作为行延续

有时反斜杠可能仍然合适。例如,长且多个with-语句不能使用隐式延续,因此可以接受反斜杠

这意味着(即使这与PEP8-E122无关),您应该将其包装在paradensis中,而不是使用反斜杠,然后隐式直线延续(缩进)是左括号:

if len(argmaxcomp) == 1:
    print("The complex with the greatest mean abundance is: {0}"
          .format(argmaxcomp[0]))
#         ^--------- The bracket opens here
只有两种例外情况提到反斜杠是可以接受的,因为偏执是不可能的(因为它们在这些上下文中有另一种含义):

  • assert
    s
但是,如果您确实想要反斜杠(仅在python2中可能),它应该与第一个表达式具有相同的缩进:

if len(argmaxcomp) == 1:
    print "The complex with the greatest mean abundance is: {0}" \
          .format(argmaxcomp[0])
#         ^--------- The first expression starts here

这种情况下的问题是根本没有压痕 很明显,错误发生在最后一行。 如果括号不是选项,只需添加缩进,如下所示:

if len(argmaxcomp) == 1:
    print "The complex with the greatest mean abundance is: {0}" \
        .format(argmaxcomp[0])

任何数量的空格都可以,但我不知道首选哪个。

我没有遇到上述错误,但我尝试了以下类型, 请张贴错误,以便我们检查

In [6]: argmaxcomp = [100]

In [7]: if len(argmaxcomp) == 1:
   ...:     print 'val: {0}'\
   ...:     .format(argmaxcomp[0])
   ...:     
val: 100

In [8]: if len(argmaxcomp) == 1:
   ...:     print 'val: {0}'.format(argmaxcomp[0])
   ...:     
val: 100

In [9]: if len(argmaxcomp) == 1:
   ...:     print 'val: {0}'.format(
   ...:     argmaxcomp[0])
   ...:     
val: 100

一般来说,pep8建议你

包装长行的首选方法是在括号、方括号和大括号内使用Python的隐含行继续。通过将表达式括在括号中,可以在多行上打断长行。应该优先使用这些选项,而不是使用反斜杠作为行延续

即:

if len(argmaxcomp) == 1:
    print("The complex with the greatest mean abundance is: {0}"
          .format(argmaxcomp[0]))
另一个选项是使用python 3的打印:

from __future__ import print_function

if len(argmaxcomp) == 1:
    print("The complex with the greatest mean abundance is:", argmaxcomp[0])

注意:打印功能可能会中断/需要更新代码的其余部分。。。您使用过print的任何地方。

刚刚遇到类似问题并解决了它。我认为OP代码的问题在于,续行之间可能有空格。那里应该没有别的东西,只有\n.