Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SyntaxError: invalid token in datetime.datetime(2012,05,22,09,03,41)?

I do something like this:

>>>import datetime >>>datetime.datetime(2012,05,22,05,03,41) datetime.datetime(2012, 5, 22, 5, 3, 41)  >>> datetime.datetime(2012,05,22,07,03,41) datetime.datetime(2012,05,22,07,03,41)  >>> datetime.datetime(2012,05,22,9,03,41) datetime.datetime(2012, 5, 22, 9, 3, 41)  >>> datetime.datetime(2012,05,22,09,03,41) SyntaxError: invalid token 

Why I get SyntaxError? How to fix it?

like image 738
user7172 Avatar asked May 22 '13 07:05

user7172


1 Answers

In Python 2, a number starting with 0 is interpreted as an octal number, often leading to confusion for those not familiar with C integer literal notations. In Python 3, you cannot start a number with 0 at all.

Remove the leading 0s:

datetime.datetime(2012, 5, 22, 9, 3, 41) 

The error is caused by 09 not being a valid octal number:

>>> 010 8 >>> 09   File "<stdin>", line 1     09      ^ SyntaxError: invalid token 
like image 93
Martijn Pieters Avatar answered Sep 22 '22 06:09

Martijn Pieters