gpt4 book ai didi

python - 我在 if 语句下写的语法错误

转载 作者:行者123 更新时间:2023-11-28 22:43:03 26 4
gpt4 key购买 nike

我正在为此苦苦挣扎,也许是我看不到但很简单的事情......

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.

关于python - 我在 if 语句下写的语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31208104/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com