我在 if 语句下写的语法错误
Syntax error whatever I write under an if statement
我正在为此苦苦挣扎,也许是我看不到但很简单的事情...
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 styleguide (PEP 8) 推荐的内容:
Tabs or Spaces?
Never mix tabs and spaces.
The most popular way of indenting Python is with spaces only. The second-most popular way is with tabs only. Code indented with a mixture of tabs and spaces should be converted to using spaces exclusively. When invoking the Python command line interpreter with the -t option, it issues warnings about code that illegally mixes tabs and spaces. When using -tt these warnings become errors. These options are highly recommended!
For new projects, spaces-only are strongly recommended over tabs. Most editors have features that make this easy to do.
我正在为此苦苦挣扎,也许是我看不到但很简单的事情...
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 styleguide (PEP 8) 推荐的内容:
Tabs or Spaces?
Never mix tabs and spaces.
The most popular way of indenting Python is with spaces only. The second-most popular way is with tabs only. Code indented with a mixture of tabs and spaces should be converted to using spaces exclusively. When invoking the Python command line interpreter with the -t option, it issues warnings about code that illegally mixes tabs and spaces. When using -tt these warnings become errors. These options are highly recommended!
For new projects, spaces-only are strongly recommended over tabs. Most editors have features that make this easy to do.