2

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?

2 Answers 2

2

Not every element in the row is a string. You can't take the len() of an int. Therefore, ensure everything is a string before taking its length.

Try making this change:

BEFORE

length_list = [len(element) for row in lst for element in row]

AFTER

length_list = [len(str(element)) for row in lst for element in row]

and also change this line too (wrap element in str():

BEFORE

row = "".join(element.ljust(column_width + 2) for element in row)

AFTER

row = "".join(str(element).ljust(column_width + 2) for element in row)
Sign up to request clarification or add additional context in comments.

2 Comments

So that got rid of the first error. But now it brings up Error: 'int' object has no attribute 'ljust' even though it was turned into a string?
Oh yeah, you have to wrap element in str() in the join statement too. I updated my answer - see above.
1

You can print lists of list as tables using python format strings:

# Input is expected as a list of list
rows = [
    ["TerritoryID", "TerritoryDescription", "RegionID"],
    ["1581", "Westboro", "1"], 
    ["1730","Bedford","1"], 
    ["1833","Georgetown","1"], 
    ["2116","Boston","1"], 
    ["2139","Cambridge","1"],
]

# First we get the max width of each column, like so:
max_col = list(max(map(len, x)) + 2 for x in list(map(list, zip(*rows))))

# Set some padding for the index column:
idx_pad = len(str(len(rows))) + 2

# Create a format string that will accept both values, and paddings:
s = "{:<{}}" + "{:<{}}" * len(max_col)

# Iterate the list of lists, printing each row:
for i, row in enumerate(rows):
    if i == 0:
        i = ""
    c = row + max_col
    c[::2] = row
    c[1::2] = max_col
    print(s.format(i, idx_pad, *c))
    idx_pad = old_idx

Which will print out:

   TerritoryID  TerritoryDescription  RegionID  
1  1581         Westboro              1         
2  1730         Bedford               1         
3  1833         Georgetown            1         
4  2116         Boston                1         
5  2139         Cambridge             1         

1 Comment

I'm still getting the object of type 'int' has no len() error, I'm guessing that's because the rows arent all strings. Would I have to convert the rows to strings and then that would fix it? @Alex

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.