Python SyntaxError 无效令牌
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36386346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SyntaxError invalid token
提问by zerocool
I have a problem when I try to assign a value to a variable. The problem shows up when I try to put a date as a tuple or a list in this order: year, month, day.
当我尝试为变量赋值时遇到问题。当我尝试按以下顺序将日期作为元组或列表放置时,问题出现了:year, month, day.
>>> a = (2016,04,03) # I try to put the date into variable 'a' as a tuple.
SyntaxError: invalid token
>>> a = [2016,04,03] # I try to put the date into variable 'a' as a list.
SyntaxError: invalid token
Why is this happing?
How do I fix it?
What does token mean in Python?
为什么会这样?
我如何解决它?
令牌在 Python 中是什么意思?
回答by Tuomas Laakkonen
In Python 3, leading zeros are not allowed on numbers. E.g:
在 Python 3 中,数字上不允许有前导零。例如:
05
0123
Etc. are not allowed, but should be written as 5
and 123
instead.
等等是不允许的,而应该写成5
and123
代替。
In Python 2, however, the leading zero signifies that the number is an octal number(base eight), so 04
or 03
would mean 4
and 3
in octal, respectively, but 08
would be invalid as it is not a valid octal number.
但是,在 Python 2 中,前导零表示该数字是八进制数(基数为 8),因此04
or分别03
表示4
和3
八进制,但08
由于它不是有效的八进制数而无效。
In Python 3, the syntax for octals changed to this:
在 Python 3 中,八进制的语法更改为:
0o10
0o4
(As well as allowing other bases such as binary and hexadecimal using the 0b
or 0x
prefixes.)
(以及允许使用0b
or0x
前缀的其他基数,例如二进制和十六进制。)
As for your other question, a token in Python is the way the Python interpreter splits up your code into chunks, so that it can understand it (see here). Here, when the tokenizer tries to split up your code it doesn't expect to see the zero there and so throws an error.
至于您的另一个问题,Python 中的令牌是 Python 解释器将您的代码拆分成块的方式,以便它可以理解它(请参阅此处)。在这里,当分词器尝试拆分您的代码时,它不希望在那里看到零,因此会引发错误。
I would suggest (similarly to the other answers) that you drop the leading zero ((2016,4,3)
) or represent these using strings (("2016","04","03")
).
我建议(与其他答案类似)删除前导零 ( (2016,4,3)
) 或使用字符串 ( ("2016","04","03")
)表示这些。
回答by chepner
04
is a valid integer literal in Python 2.x. It is interpreted as a base-8 (octal) number. 09
would be an invalid token as well, since 9
is not a valid octal digit.
04
是 Python 2.x 中的有效整数文字。它被解释为基数为 8(八进制)的数字。09
也将是一个无效的标记,因为9
它不是一个有效的八进制数字。
In Python 3, the form of octal literals changed. A leading zero alone is no longer valid; you need to explicitly specify the base. For example, 0o12
is equal to 10
.
在 Python 3 中,八进制文字的形式发生了变化。单独的前导零不再有效;您需要明确指定基数。例如,0o12
等于10
。
In your case, you probably want to just drop the leading 0: a = (2016, 4, 3)
. Leading zeros could be added to the string representation of your tuple when necessary, rather than trying to store them explicitly.
在您的情况下,您可能只想删除前导 0: a = (2016, 4, 3)
。必要时可以将前导零添加到元组的字符串表示中,而不是尝试显式存储它们。
回答by Piyush
In python version 2.7, we gets error when we use 0 before any number and that number is invalid in octal number system. For e.g. if we use 08 or 09 then we will encounter the same error 'invalid token'.
在 python 2.7 版本中,当我们在任何数字之前使用 0 时会出错,并且该数字在八进制数系统中无效。例如,如果我们使用 08 或 09,那么我们将遇到相同的错误“无效令牌”。
Python interpreter divides the entire script into various parts and those parts are called tokens. Here, 08 will be consider as token and hence it is in octal and invalid in this number system so this kind of error occurs.
Python 解释器将整个脚本分成不同的部分,这些部分称为标记。在这里,08 将被视为令牌,因此它是八进制的,在该数字系统中无效,因此会发生这种错误。
Can you please try to run a simple statement like a=04 and mention the result? If it works and fail only while using tuple or list then it may be the issue with particular python version. If it does not work then there is something wrong with your machine configuration. In this case, you can upgrade your python version if you are using the older version.
您能否尝试运行一个简单的语句,例如 a=04 并提及结果?如果它仅在使用元组或列表时工作并失败,则可能是特定 python 版本的问题。如果它不起作用,那么您的机器配置有问题。在这种情况下,如果您使用的是旧版本,则可以升级您的 python 版本。
回答by vinx.py
The problem is the 0 before the 4. If you want to store that kind of infos, try using strings.
问题是 4 之前的 0。如果您想存储那种信息,请尝试使用字符串。
a = (2016,04,03) --> Error
a = (2016,4,3) --> No Error
a = ("2016","04","03") --> No Error
a = "2016-04-03" --> you could retrieve Year, Month and Day by splitting this string
In Python 2.x 04 is interpreted as an octal number. In Python 3 octal numbers are written in form 0o4 as written here : http://docs.python.org/3.0/whatsnew/3.0.html#integers
在 Python 2.x 04 中被解释为八进制数。在 Python 3 中,八进制数以 0o4 格式书写,如下所示:http://docs.python.org/3.0/whatsnew/3.0.html#integers
回答by ATUL VARSHNEY
When we install the module then sometimes error show SyntaxError invalid token then use following command
当我们安装模块时,有时会出现错误显示 SyntaxError invalid token 然后使用以下命令
pip install --upgrade pip
pip 安装 --upgrade pip
after that then install the module using two commands
之后,然后使用两个命令安装模块
easy_install package_name
easy_install package_name
pip install package_name
pip 安装包名称