0
table = Texttable()
table.set_deco(Texttable.HEADER | Texttable.VLINES | Texttable.HLINES | Texttable.BORDER)
table.add_rows([ ["Name", "Age", "Nickname"],
                     ["Xavier Huon", 32, "Xav'"],
                     ["Baptiste Clement", 1, "Baby"] ])
print table.draw()

result:

enter image description here

how i can make this?

enter image description here

Robby Zhi
  • 29
  • 2

1 Answers1

2

There's no real good way to do it with texttable. For instance, it doesn't seem to understand there can be additional grouped lists underneath a main header.

Instead, you can accomplish this with some clever formatting:

>>> table = Texttable()
>>> table.set_deco(Texttable.HEADER | Texttable.VLINES | Texttable.HLINES | Texttable.BORDER)
>>> 
>>> table.add_rows([ ["Name\nfirst   |   last", "Age", "Nickname"],
...                      ["Xavier   |    Huon", 32, "Xav'"],
...                      ["Baptiste | Clement", 1, "Baby"] ])
>>> print table.draw()
+--------------------+-----+----------+
|        Name        | Age | Nickname |
|  first   |   last  |     |          |
+====================+=====+==========+
| Xavier   |    Huon | 32  | Xav'     |
+--------------------+-----+----------+
| Baptiste | Clement | 1   | Baby     |
+--------------------+-----+----------+

The only issue would be aligning the first and last name items should the table grow larger with a longer name. You could however do some math to determine the largest name, then use string formatting to left and right align it. I'll leave that as an exercise to the reader (and apparently double poster).

Community
  • 1
  • 1
VooDooNOFX
  • 4,674
  • 2
  • 23
  • 22