AlvinL
AlvinL

Reputation: 458

Table creation with csv data

Given a csv with the contents of something like:

Colour, Red, Black, Blue
Taste, Good, Bad, Disgusting
Smell, Pleasant, Deceptive, Intolerable

How can I print this out in python so that it would look like this:

+-------+-----------+-----------+
|Colour |Taste      | Smell     |
+-------+-----------+-----------+
|  Red  |Good       | Pleasant  |
| Black | Bad       | Deceptive |
| Blue  | Disgusting|Intolerable|
+-------+-----------+-----------+

Do I have to create the table manually with +'s -'s and |'s taking into account respective column's longest strings or is there a built-in method for this? I did search for python tables, but nothing relevant to the problem came up. Also the example table that I typed in manually is not symmetric in every cell (not "aligned" properly).

Crux of the problem is the +-| table creation.

What to do?

Upvotes: 5

Views: 279

Answers (2)

Padraic Cunningham
Padraic Cunningham

Reputation: 180550

The closest thing to a built-in method is using str.format:

import csv
with open("output.txt") as f:
    lines = list(csv.reader(f,delimiter=","))
    # get longest string for alignment
    mx_len = len(max((max(ele,key=len) for ele in lines),key=len))
    # transpose the list items
    zipped = zip(*lines)
    # get header/first row 
    row1 = zipped[0]
    # how many "-" we need depends on longests word length
    pattern = "-"*mx_len
    f = ("+{pat}+{pat}+{pat}+".format(pat=pattern))
    print(f)
    # pass in mx_len as align value
    print("|{:<{i}}|{:<{i}}|{:<{i}}|".format(*row1,i=mx_len))
    print(f)
    # print the rest of the transposed data excluding column 1/row1
    for a, b, c in zipped[1:]:
        print("|{:<{i}}|{:<{i}}|{:<{i}}|".format(a.rstrip(),b.rstrip(),c.rstrip(),i=mx_len))
    print(f)

+------------+------------+------------+
|Colour      |Taste       |Smell       |
+------------+------------+------------+
| Red        | Good       | Pleasant   |
| Black      | Bad        | Deceptive  |
| Blue       | Disgusting | Intolerable|
+------------+------------+------------+

Without know exactly how many cols are in the file:

with open("output.txt") as f:
    lines = list(csv.reader(f, delimiter=","))
    mx_len = len(max((max(ele, key=len) for ele in lines), key=len))
    zipped = zip(*lines)
    row1 = zipped[0]
    ln = len(row1)
    pattern = "-" * mx_len
    f = (("+{pat}" * ln + "+").format(pat=pattern))
    print(f)
    print(("|{:<{i}}" * ln + "|").format(*row1, i=mx_len))
    print(f)
    for row in zipped[1:]:
        print(("|{:<{i}}" * ln + "|").format(*row, i=mx_len))
    print(f)

+------------+------------+------------+
|Colour      |Taste       |Smell       |
+------------+------------+------------+
| Red        | Good       | Pleasant   |
| Black      | Bad        | Deceptive  |
| Blue       | Disgusting | Intolerable|
+------------+------------+------------+

Upvotes: 3

elyase
elyase

Reputation: 41013

This is not built-in but you can use terminaltables:

from terminaltables import AsciiTable

with open('test.csv') as f:
    table_data = [line.split(",") for line in f]
    transposed = [list(i) for i in zip(*table_data)] 

print(AsciiTable(transposed).table)

To install just do:

pip install terminaltables

Upvotes: 3

Related Questions