Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Table creation with csv data

Tags:

python

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?

like image 875
AlvinL Avatar asked Jan 11 '15 12:01

AlvinL


People also ask

How to create a table from a CSV file?

In this process first we create a csv file with data and then use that file for creating a table. This functionality uses only CSV and TXT files. When creating a CSV file do not leave any blank cells; try to delete them. Create your CSV file with data. Your Header name will use a Table Field Name.

How to create a wpdatatable from a CSV file?

After you saved the CSV file with your data, the next step is to go to your WordPress admin and create a new wpDataTable from the CSV data source. 1. Go to wpDataTables -> Create a Table, and choose Create a table linked to an existing data source option. 2. Set a name for your new wpDataTable that will help you to find the table later. 3.

How to create a CSV-based table in WordPress?

Please note that the default CSV delimiter is comma ( , ) but in wpDataTables settings, you can change it to: The first thing you will need to do to create a CSV-based table in WordPress is to create the CSV file with your data set in your favorite software e.g., MS Excel, OpenOffice, LibreOffice, Numbers, Google Spreadsheet, or any other.

How to create a CSV file in SQL Server management studio?

When creating a CSV file do not leave any blank cells; try to delete them. Create your CSV file with data. Your Header name will use a Table Field Name. You can see in the below image I created a file with the following data. Open your SQL Server Management Studio. Here SQL Server Management Studio V18.6. Create a database if you do not have any.


2 Answers

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|
+------------+------------+------------+
like image 163
Padraic Cunningham Avatar answered Sep 27 '22 23:09

Padraic Cunningham


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
like image 26
elyase Avatar answered Sep 27 '22 22:09

elyase