SyntaxError 无效令牌 [英] SyntaxError invalid token
问题描述
当我尝试为变量赋值时遇到问题.当我尝试按以下顺序将日期作为元组或列表放置时,问题出现了:年、月、日.
为什么会这样?
我该如何解决?
token 在 Python 中是什么意思?
在 Python 3 中,数字上不允许有前导零.例如:
050123
等等.不允许,但应写为 5
和 123
代替.
然而,在 Python 2 中,前导零表示该数字是一个八进制数(基数为 8),因此 04
或 03
分别表示 4
和 3
的八进制数,但 08
无效,因为它不是有效的八进制数.
在 Python 3 中,八进制的语法更改为:
0o100o4
(以及允许使用 0b
或 0x
前缀的其他基数,例如二进制和十六进制.)
至于您的另一个问题,Python 中的令牌是 Python 解释器将您的代码分成块的方式,以便它可以理解它(请参阅 此处).在这里,当分词器尝试拆分您的代码时,它不希望在那里看到零,因此会引发错误.
我建议(与其他答案类似)删除前导零 ((2016,4,3)
) 或使用字符串 (("2016","04","03")
).
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.
>>> 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?
In Python 3, leading zeros are not allowed on numbers. E.g:
05
0123
Etc. are not allowed, but should be written as 5
and 123
instead.
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.
In Python 3, the syntax for octals changed to this:
0o10
0o4
(As well as allowing other bases such as binary and hexadecimal using the 0b
or 0x
prefixes.)
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.
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")
).
这篇关于SyntaxError 无效令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!