Whenever the mistake TypeError ... object is not subscriptable
appears is why the code is trying to use the operator [ ]
on an object that is not a sequence or a map.
And in your case, on the line
self.date1 = datetime.strptime(self.date1,'%d/%m/%Y').date()
You do the attribute self.date1
have a date (a datetime.date
Python). It would even make sense for date-type objects to function as sequences (and thus, could be used with [...]
) they could be a sequence of year, month, day. But they are not. And it does not come to the case, why in the line that carries your error you put:
if self.date1[0:3] >= 2018:
That is, you want to look at the first 4 elements of the "sequence" self.date1
, as if it were a string. But it is not a string, it is an object of type date. If you want the year, just take the attribute .year
:
if self.date1.year >= 2018:
And you won’t have that mistake anymore. The SQL Standard stores the date so that it can function as a string and as a date in a mixed way - because historically it was like this. But with the Python side your data is Python objects, and dates are dates, not strings - so it can’t be cut out like a string, with [0:3]
.
Another tip is, when you have an error, read the error message carefully - in addition to the part that you placed in the question, in the previous lines Python displays the exact line that gave error - and that previous functions called the current function where the error occurred. Perhaps you would have been able to find the problem if you had looked at the correct line. But if you still prefer to ask, remember to paste the whole error message - in a more complex case it can be difficult to guess the error just with the error message, without knowing where it occurred.
(tip 2: "month" in English doesn’t have "u"- it’s just "Month". And it’s not like some words in British English have the "u", like "Colour").
I think the mistake is in
self.date1[0:3]
. If you want the year, andself.date1
is a datetime, I imagine the right thing to doself.date1.year
.– AlexCiuffa
Typeerror: 'int' Object is not callable
– Felipe Maia
You made that mistake there, I have to change 2018 now.
– Felipe Maia
tried to put as string, but gave this error
– Felipe Maia
Typeerror: 'int' Object is not callable
– Felipe Maia
If you do
if self.date1.year >= 2018:
he gives the errorTypeError: 'int' object is not callable
?– AlexCiuffa
worked out here, thank you!
– Felipe Maia
another error, which will be on another line (and is not wrong in the code above). See the response below - includes a part that talks about reading an error message.
– jsbueno