Error with date time

Asked

Viewed 139 times

0

Guys I’m trying to run my code, I’m having the following error: Typeerror: 'datetime.date' Object is not subscriptable

How do I fix it?

follow my code below:

def making_date(self, *args, **kwargs):
    self.result = c.execute("SELECT * from inventory")

    for row in self.result:
        self.date1 = row[3]
        self.date2 = row[4]

        self.date1 = datetime.strptime(self.date1,'%d/%m/%Y').date()
        self.date2 = datetime.strptime(self.date2,'%d/%m/%Y').date()

        if self.date1[0:3] >= 2018:
            self.minus = abs((self.date1 - self.date2).days)
            self.mounth = self.minus // 30
            self.year = self.mounth // 12
            print(self.year)

I’ll send the photo from my database if you help:

inserir a descrição da imagem aqui

  • I think the mistake is in self.date1[0:3]. If you want the year, and self.date1 is a datetime, I imagine the right thing to do self.date1.year.

  • Typeerror: 'int' Object is not callable

  • You made that mistake there, I have to change 2018 now.

  • tried to put as string, but gave this error

  • Typeerror: 'int' Object is not callable

  • If you do if self.date1.year >= 2018: he gives the error TypeError: 'int' object is not callable?

  • worked out here, thank you!

  • 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.

Show 3 more comments

1 answer

2

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").

Browser other questions tagged

You are not signed in. Login or sign up in order to post.