Python 无论我在if语句下写什么,都会出现语法错误
我正在为这件事挣扎,也许是我看不见但很简单的事情Python 无论我在if语句下写什么,都会出现语法错误,python,arrays,list,syntax,split,Python,Arrays,List,Syntax,Split,我正在为这件事挣扎,也许是我看不见但很简单的事情 import subprocess, pprint cmd = subprocess.Popen('bhosts', shell=True, stdout=subprocess.PIPE) errorCode = 0 description ="" arrayprova=[] for linea in cmd.stdout: if "ok" not in linea and "closed" not in linea and "HOST_
import subprocess, pprint
cmd = subprocess.Popen('bhosts', shell=True, stdout=subprocess.PIPE)
errorCode = 0
description =""
arrayprova=[]
for linea in cmd.stdout:
if "ok" not in linea and "closed" not in linea and "HOST_NAME" not in linea:
arrayprova = linea.split()
description = description + "host " + arrayprova[0] + "is " + arrayprova[1]
errorCode = 1
print arrayprova[1]
if errorCode == 0:
description ="Everything is just fine."
print description
我得到这个错误:
File "bhosts_nodes_check.py", line 9
description = description + "host " + arrayprova[0] + "is " + arrayprova[1]
^
SyntaxError: invalid syntax
您正在编辑器中混合选项卡和空格:
>>> '''\
... arrayprova = linea.split()
... description = description + "host " + arrayprova[0] + "is " + arrayprova[1]
... '''
' arrayprova = linea.split()\n\t\tdescription = description + "host " + arrayprova[0] + "is " + arrayprova[1] \n'
>>> # ^^^ spaces here - but tabs here ^^^^
...
Python将选项卡扩展到每第8列,但您的编辑器可能设置为仅使用4个空格作为选项卡,这进一步增加了混淆。您的arrayprova
行缩进到8个空格,而下一行的两个选项卡扩展到16个空格
永远不要使用混合缩进样式;只能使用制表符或空格
您可以将大多数编辑器配置为仅将空格用于缩进,按TAB键将导致写入空格。这是委员会的建议:
制表符还是空格?
不要把标签和空格混在一起
Python缩进最常用的方法是仅使用空格。第二种最流行的方式是只使用标签。混合使用制表符和空格缩进的代码应转换为仅使用空格。当使用-t选项调用Python命令行解释器时,它会发出关于非法混合制表符和空格的代码的警告。使用-tt时,这些警告将变成错误。强烈推荐这些选项
对于新项目,强烈建议在选项卡上使用空格。大多数编辑器都有一些特性,使这项工作变得容易
希望一个制表符按钮的笔划能在所有编辑器中产生四个空格。@MalikBrahimi:这很容易配置。