最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - How to place tables next to each other with tabulate - Stack Overflow

programmeradmin4浏览0评论

I am trying to use tabulate and MySQL to make and anize a table of Pokémon cards. unfortunately, the method I am using (different tables for each type) makes the tables hard to find once printed out. Does anyone know how to make three of the tables align with each other horizontally?

EDIT: Whoops, I seem to have written my question badly. I was hoping for something in the format of

+-------+------+  +------+------+
|Sun    |Earth |  |Moon  |Mars  |
|-------|------|  |------|------|
|696000 |6371  |  |1737  |3390  |
+-------+------+  +------+------+

But thanks for the help!

I am trying to use tabulate and MySQL to make and anize a table of Pokémon cards. unfortunately, the method I am using (different tables for each type) makes the tables hard to find once printed out. Does anyone know how to make three of the tables align with each other horizontally?

EDIT: Whoops, I seem to have written my question badly. I was hoping for something in the format of

+-------+------+  +------+------+
|Sun    |Earth |  |Moon  |Mars  |
|-------|------|  |------|------|
|696000 |6371  |  |1737  |3390  |
+-------+------+  +------+------+

But thanks for the help!

Share edited Mar 11 at 22:40 arggbl asked Mar 11 at 1:07 arggblarggbl 36 bronze badges 2
  • it may need to do it on your own. It may need to create 3 tables as strings (or list of lines) and later use zip(table1, table2, table3) to group all first lines, all second lines, etc. and concatenate all first line into one string and display it, concatenate all second lines and display it, etc. Problem is when tables have different number of lines - because zip() will stop when one of table will not have more lines - and it may need zip_longest(). Of course it may other problem when shorter is first or second line because you would have to add extra spaces with correct width – furas Commented Mar 11 at 10:34
  • maybe it would be simpler to use modules like modern rich, Textualize or older ncurse to create TUI (Text User Interface) – furas Commented Mar 11 at 10:38
Add a comment  | 

1 Answer 1

Reset to default 0

It may need to do it on your own.

First use tabulate to create 3 tables as strings, next convert them lists of lines, and later use zip(lines1, lines2, lines3) to group all first lines, all second lines, etc. and concatenate all first line into one string and display it, concatenate all second lines and display it, etc.

Here example using data from tabulate

Because some lines in table are shorter so it needs to get all lenghts and find the longest line in table, and later use spaces in other lines to get the same length. I use f-string {variable:length} and in place of length I put another variable.

import tabulate

table1 = [
    ["Sun",696000,1989100000],
    ["Earth",6371,5973.6],
]    

table2 = [
    ["Moon",1737,73.5],
    ["Mars",3390,641.85],
]

table3 = [
    ["Hello",1234,73.5],
    ["World",5678,641.85],
]

tab1 = tabulate.tabulate(table1)
lines1 = tab1.split('\n')
len1 = [len(x) for x in lines1]
max1 = max(len1)

tab2 = tabulate.tabulate(table2)
lines2 = tab2.split('\n')
len2 = [len(x) for x in lines2]
max2 = max(len2)

tab3 = tabulate.tabulate(table3)
lines3 = tab3.split('\n')
len3 = [len(x) for x in lines3]
max3 = max(len3)

for item1, item2, item3 in zip(lines1, lines2, lines3):
    print(f'{item1:{max1}} | {item2:{max2}} | {item3:{max3}}')

Result:

-----  ------  ------------- | ----  ----  ------ | -----  ----  ------
Sun    696000     1.9891e+09 | Moon  1737   73.5  | Hello  1234   73.5
Earth    6371  5973.6        | Mars  3390  641.85 | World  5678  641.85
-----  ------  ------------- | ----  ----  ------ | -----  ----  ------

Problem is when tables have different number of line because zip() will stop when one of table will not have more lines. And it may need to use zip_longest (with fillvalue="") from module itertools.

import tabulate
from itertools import zip_longest

table1 = [
    ["Sun",696000,1989100000],
    ["Earth",6371,5973.6],
]    

table2 = [
    ["Sun",696000,1989100000],
    ["Earth",6371,5973.6],
    ["Moon",1737,73.5],
    ["Mars",3390,641.85],
]

table3 = [
    ["Hello",1234,73.5],
    ["World",5678,641.85],
]

tab1 = tabulate.tabulate(table1)
lines1 = tab1.split('\n')
len1 = [len(x) for x in lines1]
max1 = max(len1)

tab2 = tabulate.tabulate(table2)
lines2 = tab2.split('\n')
len2 = [len(x) for x in lines2]
max2 = max(len2)

tab3 = tabulate.tabulate(table3)
lines3 = tab3.split('\n')
len3 = [len(x) for x in lines3]
max3 = max(len3)

for item1, item2, item3 in zip_longest(lines1, lines2, lines3, fillvalue=""):
    print(f'{item1:{max1}} | {item2:{max2}} | {item3:{max3}}')

Result:

-----  ------  ------------- | -----  ------  ------------- | -----  ----  ------
Sun    696000     1.9891e+09 | Sun    696000     1.9891e+09 | Hello  1234   73.5
Earth    6371  5973.6        | Earth    6371  5973.6        | World  5678  641.85
-----  ------  ------------- | Moon     1737    73.5        | -----  ----  ------
                             | Mars     3390   641.85       |
                             | -----  ------  ------------- |
发布评论

评论列表(0)

  1. 暂无评论