Anuj Gautam
Anuj Gautam

Reputation: 1255

Printing Lists as Tabular Data, Group Rows

I need to format a data containing as list of lists in a table. I can make a grid using tabulate:

x = [['Alice', 'min', 2],
     ['', 'max', 5],
     ['Bob', 'min', 8],
     ['', 'max', 15]]
header = ['Name', '', 'value']
print(tabulate.tabulate(x, headers=header, tablefmt="grid"))
+--------+-----+---------+
| Name   |     |   value |
+========+=====+=========+
| Alice  | min |       2 |
+--------+-----+---------+
|        | max |       5 |
+--------+-----+---------+
| Bob    | min |       8 |
+--------+-----+---------+
|        | max |      15 |
+--------+-----+---------+

However, we require grouping of rows, like this:

+--------+-----+---------+
| Name   |     |   value |
+========+=====+=========+
| Alice  | min |       2 |
+        +     +         +
|        | max |       5 |
+--------+-----+---------+
| Bob    | min |       8 |
+        +     +         +
|        | max |      15 |
+--------+-----+---------+

I tried using multiline rows (using "\n".join()), which is apparently supported in tabular 0.8.3, with no success.

This is required to run in the production server, so we can't use any heavy libraries. We are using tabulate because the whole tabulate library is a single file, and we can ship the file with the product.

Upvotes: 2

Views: 372

Answers (1)

DucNguyen
DucNguyen

Reputation: 21

You can try this:

x = [['Alice', 'min\nmax', '2\n5'],
    ['Bob', 'min\nmax', '8\n15'],
    ]
+--------+-----+------------------------+
| Name   |     | ['value1', 'value2']   |
+========+=====+========================+
| Alice  | min | 2                      |
|        | max | 5                      |
+--------+-----+------------------------+
| Bob    | min | 8                      |
|        | max | 15                     |
+--------+-----+------------------------+

Upvotes: 2

Related Questions