python 的新手问题,脚本在其他 os 上运行良好
Newbie issue with python, script works fine on other os
我正在使用 python 的机器人在网站上创建帐户。当我在安装了 python 的 windows 机器上使用它时,它工作正常。我昨天 (Ubuntu 16.04) 买了一个 linux VPS,我的脚本显然不再工作了。这是我在 linux 机器上遇到的错误:
Traceback (most recent call last):
File "roblox.py", line 2, in <module>
from utils import *
File "/home/py/newbot/utils.py", line 18
key, *values = line.split(" ")
^
SyntaxError: invalid syntax
它引用的脚本中的行是这个
def string_to_dict(headers):
headers_dict = {}
for line in headers.split("\n"):
if not line: continue
line = line.strip()
key, *values = line.split(" ")
key = key[:-1]
if not (key and values): continue
headers_dict[key] = " ".join(values)
return headers_dict
知道哪里出了问题吗?
此语法在 python 2 中无效。运行 您的脚本 python3
。
正如其他答案中提到的,您需要在 Python3 中 运行 这个。最短的答案是:
安装 python3,您的机器上可能还没有:$ sudo apt-get update; sudo apt-get install python3
养成在脚本文件中使用 shebang 的习惯。将 #!/usr/bin/env python3
添加到脚本的第一行以指示 shell 调用 python3 而不是 python2 -- 大多数系统都带有 /usr/bin/python 的别名 python2 安装。
您可以在 Bash shell(Ubuntu 16.04 的默认 shell)here.[=14= 中找到有关源文件的更多信息]
更长的答案——如果您想知道为什么代码在 Python2 中不起作用但在 Python3 中起作用——是 Python3 引入了扩展的可迭代拆包在 PEP 3132 中,但此功能未在 Python2 中实现。
我正在使用 python 的机器人在网站上创建帐户。当我在安装了 python 的 windows 机器上使用它时,它工作正常。我昨天 (Ubuntu 16.04) 买了一个 linux VPS,我的脚本显然不再工作了。这是我在 linux 机器上遇到的错误:
Traceback (most recent call last):
File "roblox.py", line 2, in <module>
from utils import *
File "/home/py/newbot/utils.py", line 18
key, *values = line.split(" ")
^
SyntaxError: invalid syntax
它引用的脚本中的行是这个
def string_to_dict(headers):
headers_dict = {}
for line in headers.split("\n"):
if not line: continue
line = line.strip()
key, *values = line.split(" ")
key = key[:-1]
if not (key and values): continue
headers_dict[key] = " ".join(values)
return headers_dict
知道哪里出了问题吗?
此语法在 python 2 中无效。运行 您的脚本 python3
。
正如其他答案中提到的,您需要在 Python3 中 运行 这个。最短的答案是:
安装 python3,您的机器上可能还没有:
$ sudo apt-get update; sudo apt-get install python3
养成在脚本文件中使用 shebang 的习惯。将
#!/usr/bin/env python3
添加到脚本的第一行以指示 shell 调用 python3 而不是 python2 -- 大多数系统都带有 /usr/bin/python 的别名 python2 安装。
您可以在 Bash shell(Ubuntu 16.04 的默认 shell)here.[=14= 中找到有关源文件的更多信息]
更长的答案——如果您想知道为什么代码在 Python2 中不起作用但在 Python3 中起作用——是 Python3 引入了扩展的可迭代拆包在 PEP 3132 中,但此功能未在 Python2 中实现。