I'm working with a database and I'm trying to list all the tables in that one database. I can print them fine, but I'm having trouble formatting the tables correctly. I used pandas to format it and that works, but I'm trying to create this without pandas. This is how it is printing out so far:
TerritoryID TerritoryDescription RegionID
1581 Westboro 1
1730 Bedford 1
1833 Georgetown 1
2116 Boston 1
2139 Cambridge 1
I'm trying to get it to look something like this:
TerritoryID TerritoryDescription RegionID
1. 1581 Westboro 1
2. 1730 Bedford 1
3. 1833 Georgetown 1
4. 2116 Boston 1
5. 2139 Cambridge 1
What I've tried is finding the maximum length of the lists and formatting them that way, since there are other tables I'm trying to format. This is what I tried doing, but, I get an error that says: object of type 'int' has no len()
def categories(menu, choice, cursor, connection):
sql = "SELECT * FROM " + menu[choice - 1]
cursor.execute(sql)
rows = cursor.fetchall()
lst = [list(elem) for elem in rows]
connection.close()
return lst
def columns(lst, cursor):
header = []
for field in cursor.description:
header.append(field[0])
print(' '.join(header))
length_list = [len(element) for row in lst for element in row]
column_width = max(length_list)
for row in lst:
row = "".join(element.ljust(column_width + 2) for element in row)
print(row)
How would I fix this error? Or is there another way to do this?