python的新手问题,脚本在其他操作系统上运行正常

时间:2017-02-14 19:27:14

标签: python linux bots

我正在使用python的bot来在网站上创建帐户。当我在安装了python的Windows机器上使用它时,它工作正常。我昨天买了一个Linux VPS(Ubuntu 16.04),我的脚本显然不再工作了。这是我在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

任何可能出错的想法?

2 个答案:

答案 0 :(得分:1)

此语法在python 2中无效。使用python3运行脚本。

答案 1 :(得分:0)

如其他答案中所述,您需要在Python3中运行它。最简短的答案是:

  1. 安装python3,它可能不在您的计算机上:$ sudo apt-get update; sudo apt-get install python3

  2. 养成在脚本文件中使用shebang的习惯。将#!/usr/bin/env python3添加到脚本的第一行,以指示shell调用python3而不是python2 - 大多数系统都带有/ usr / bin / python别名为python2安装。

  3. 您可以在Bash shell中找到更多有关采购文件的信息(Ubuntu 16.04的默认shell)here

    更长的答案 - 如果你想知道为什么代码在Python2中不起作用但在Python3中起作用 - 是Python3在PEP 3132中引入了扩展的可迭代解包,但是这个特性没有在Python2。