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.
},
]
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. |
+----+--------------------+--------------------------------------------+
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With