D
D
Denis2021-11-22 21:46:00
Python
Denis, 2021-11-22 21:46:00

How to fix ValueError: time data "('2021-11-20',)" does not match format '%y-%m-%d'?

Hello, I'm trying to convert a date from the database into a string in order to compare them ">="
It gives the following error
619be50211fa0954816448.png

Code:

@tasks.loop(seconds=2.0)
async def ban_loop():
  getdatanow = datetime.now().date()
  for ban in sql.execute(F"SELECT days FROM bans"):
    getdays = str(ban)
    ban1 = datetime.strptime(getdays, "%y-%m-%d")
    if getdatanow >= ban1:
      sql.execute(f"UPDATE bans SET ex = 'True'")
      for bans in sql.execute(f"SELECT FROM bans WHERE ex = 'True'"):
        if getdatanow > bans[2]:
          guild = client.get_guild(806633419865718784)
          await guild.unban(getallbannedid[4])
          print(getallbannedid[4])
          sql.execute(f"DELETE FROM bans WHERE banned = {bans[0]}")
          db.commit()
          print('Пользователь разбанен по истечению длительности наказания')


The bans database
619be53824656637852534.png

How the line is written:
619be54a2157d684259400.png

Don't look at ex, it has nothing to do with it.

Please help.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
soremix, 2021-11-22
@nosemka

You don't have to do that getdays = str(ban). A query to the database gives you a tuple of data, instead of getting an element of this tuple, for some reason you turn it into a string, and you get this ( string ) ('2021-11-20', ).
You are trying to parse this string according to the format "year-month-day", while your string looks like "bracketquote-year-month-dayquotecommabracket" does not match so to speak.
Plus, the wrong types were used. cheatsheet , scroll down to "Format Code List"
ban1 = datetime.strptime(ban[0], "%Y-%m-%d")

G
galaxy, 2021-11-22
@galaxy

datetime.strptime(getdays, "%Y-%m-%d")
(%y -> %Y if unclear)

V
Vladimir Kuts, 2021-11-22
@fox_12

Maybe it's worth extracting the value from the tuple before converting it to a string and then converting it to a date?

...
getdays = ban[0]
ban1 = datetime.strptime(getdays, "%Y-%m-%d")
...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question