Search code examples
pythonmysqldatetimemysql-pythonpython-datetime

When displaying data from mysql in python, time and date column looks almost unreadable.. Is there a way to display it in date and time format?


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.


Solution

  • Read about strftime(format) method, to create a string representing the time under the control of an explicit format string.