Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add multiple line in python single table cell

I am not sure how to draw a table like as below, I tried using prettytable but not able to put multiple line in one cell.

NB: Number of lines should be based on the number of the string, so I want to put some n string per line.

Could some one please help?

+---- +-------------------+-------------------------------------------------------+
| Id  | Name              | Comment                                               |
+-----+-------------------+-------------------------------------------------------+
| 1   |  Alvina Skiles    | Dolor qui rerum est sed. Sed ipsa repudiandae et      |
|     |                   | Non explicabo voluptas impedit rerum dignissimos.     |
|     |                   | Minima voluptatibus sint voluptates similique.'       |
+-----+-------------------+-------------------------------------------------------+
| 2   |  Chasity Lakin    | Nesciunt ea voluptatem rerum eos rerum ut soluta      |
|     |                   | Animi totam rerum fugiat consectetur odio et          |
|     |                   | repellendus                                           |
+-----+-------------------+-------------------------------------------------------+
| 3   | Miss Brennan Kiehn| Nulla placeat saepe voluptatem molestias dolores ex   |
|     |                   | Reiciendis nostrum adipisci qui enim explicabo.       |
+-----+-------------------+-------------------------------------------------------+

Here is my data structure to construct the table:

[
    {
        "id": "1",
        "name": "Alvina Skiles",
        "comment": 'Dolor qui rerum est sed. Sed ipsa repudiandae et. Non explicabo voluptas impedit rerum dignissimos. Minima voluptatibus
    },
    {
        "id": "2",
        "name" : 'Chasity Lakin',
        "comment": 'Nesciunt ea voluptatem rerum eos rerum ut soluta. Animi totam rerum fugiat consectetur odio et repellendus.',
    },
    {
        "id": "3",
        "name" : 'Miss Brennan Kiehn',
        "comment": 'Nulla placeat saepe voluptatem molestias dolores ex. Reiciendis nostrum adipisci qui enim explicabo.
    },
]
like image 589
James Avatar asked Mar 21 '14 13:03

James


1 Answers

It seems that prettytable can handle line breaks pretty well, so a quick formatting function may just do the trick. Here's my sample:

import prettytable
from prettytable import ALL as ALL
items_table = prettytable.PrettyTable(hrules=ALL)
items_table.field_names = ["id", "name", "comment"]

items = [
    {
        "id": "1",
        "name": "Alvina Skiles",
        "comment": 'Dolor qui rerum est sed. Sed ipsa repudiandae et. Non explicabo voluptas impedit rerum dignissimos. Minima voluptatibus'
    },
    {
        "id": "2",
        "name" : 'Chasity Lakin',
        "comment": 'Nesciunt ea voluptatem rerum eos rerum ut soluta. Animi totam rerum fugiat consectetur odio et repellendus.'
    },
    {
        "id": "3",
        "name" : 'Miss Brennan Kiehn',
        "comment": 'Nulla placeat saepe voluptatem molestias dolores ex. Reiciendis nostrum adipisci qui enim explicabo.'
    },
]

def format_comment(comment, max_line_length):
    #accumulated line length
    ACC_length = 0
    words = comment.split(" ")
    formatted_comment = ""
    for word in words:
        #if ACC_length + len(word) and a space is <= max_line_length 
        if ACC_length + (len(word) + 1) <= max_line_length:
            #append the word and a space
            formatted_comment = formatted_comment + word + " "
            #length = length + length of word + length of space
            ACC_length = ACC_length + len(word) + 1
        else:
            #append a line break, then the word and a space
            formatted_comment = formatted_comment + "\n" + word + " "
            #reset counter of length to the length of a word and a space
            ACC_length = len(word) + 1
    return formatted_comment

for item in items:
    item["comment"] = format_comment(item["comment"], 42)
    items_table.add_row([item["id"], item["name"], item["comment"]])

print(items_table)

And here's the output:

+----+--------------------+--------------------------------------------+
| id |        name        |                  comment                   |
+----+--------------------+--------------------------------------------+
| 1  |   Alvina Skiles    |     Dolor qui rerum est sed. Sed ipsa      |
|    |                    |  repudiandae et. Non explicabo voluptas    |
|    |                    |     impedit rerum dignissimos. Minima      |
|    |                    |               voluptatibus                 |
+----+--------------------+--------------------------------------------+
| 2  |   Chasity Lakin    | Nesciunt ea voluptatem rerum eos rerum ut  |
|    |                    |     soluta. Animi totam rerum fugiat       |
|    |                    |     consectetur odio et repellendus.       |
+----+--------------------+--------------------------------------------+
| 3  | Miss Brennan Kiehn | Nulla placeat saepe voluptatem molestias   |
|    |                    |  dolores ex. Reiciendis nostrum adipisci   |
|    |                    |            qui enim explicabo.             |
+----+--------------------+--------------------------------------------+
like image 147
rolandog Avatar answered Oct 30 '22 12:10

rolandog