I want to display a mysql table in Python, when I use the "Select * from table" command, the time and date column is displayed as a bunch of numbers with commas in between. Its hard to read them as date and time. My code is;
from datetime import datetime
import time
import mysql.connector as sq
s = sq.connect(host="localhost", user="root", passwd="Zxcvbnm*1", database="ip_project")
if s.is_connected():
print("Sucess")
try:
print("Displaying Attendance..")
cur=s.cursor()
d="""select * from main"""
cur.execute(d)
data=cur.fetchall()
for i in data:
print(i)
except Exception:
print("Something went wrong.. try again")
And the output is;
Sucess
Displaying Attendance..
(1, 'Talia', datetime.datetime(2021, 8, 16, 16, 28, 12))
I want to get rid of "datetime.datetime" and want it to look somewhat like
(1,'Talia',2021:8:16 16:28:12)
Is it possible? Also sorry for messy work. Just a beginner here, need this for school project.
print(i)you do not specify the way you want to see the output, so you get the Python default, which is to display the tuple as it would be represented as a literal in Python code. If you want to get rid ofdatetime.datetime, you need to extract the datetime from the tuple, asi[2], then convert that to a string in the format you want, asi[2].isoformat(). If you want some other format, usei[2].strftime(...). What you put in the parens depends on what format you want. You need to look at the documentation for that. So,print (f"({i[0]}, {i[1]!r}, {i[2].isoformat()})")