What is the easiest way to print the result from MySQL query in the same way MySQL print them in the console using Python? For example I would like to get something like tha
You need to do two passes:
So
table = cur.fetchall()
widths = [0]*len(table[0]) # Assuming there is always one row
for row in table:
widths = [max(w,len(c)) for w,c in zip(widths,row)]
Now you can print the table trivially.
Remember the string.rjust method when printing the numbers.
Update
A more functional way of calculating widths is:
sizetable = [map(len,row) for row in table]
widths = map(max, zip(*sizetable))