I am drawing a table using ascii, which does not close well, my code is the following:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import pymysql.cursors
import pymysql
connection = pymysql.connect(host='localhost', user='root', db='stack',password='toor')
c = connection.cursor()
c.execute("select * from test;")
res = c.fetchall()
print("╔═════╦══════════════════╗")
print("║ ID ║ NOMBRE ║")
print("╠═════╬══════════════════╣")
print("║ ║ ║")
for id, name in res:
print '║ '+str(id)+ ' ║' + str(name) + ' ║'
print '╚═════╩══════════════════╝ '
my result:
The idea is that the table is completely closed, but I don't know why it doesn't, I even thought it was due to existing spaces in the DB in the name but it doesn't exist, I appreciate any suggestion..!!
Diego, the problem is that the names will have different lengths and, therefore, the spaces on the right will depend on how long the name is.
It occurs to me that the simplest thing is to define a width for each column and work on these values:
In the script, I have defined a width for each column in such a way that it is a constant, using
str.ljust
you can align the text using the defined spaces. The result:If you play with the values of the widths, for example 20 and 40, it changes the way the result looks: