Python Forum
printing text tables with consistent width
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
printing text tables with consistent width
#1
is there a facility that exists to make it easy to print out a multi-column table which has data of different width in each row of a column in a way where every row in a given column has the same width? the difficult part will be that the first row has to be delayed until the lat row is output, buffering the entire table, unless the case is that the data can be produced twice.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
I've done this a few times. Get the string output as a list of lists. For each index in the sub-lists, get maximum width. Construct a format string with those widths and the justifications you want. Apply the rows to that format string repeatedly, printing or joining with '\n'.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Here's a simple example I whipped up:

def table(rows):
	max_widths = []
	for column in zip(*rows):
		max_widths.append(max([len(text) for text in column]))
	template = '  '.join(['{{:<{}}}'.format(width) for width in max_widths])
	return '\n'.join([template.format(*row) for row in rows])

knights = [['Sir Lancelot', 'The Holy Grail', 'Blue'],
	['Sir Robin', 'The Holy Grail', "I don't know that."],
	['Sir Galahad', 'The Grail', 'Blue. No, yel...'],
	['King Arthur', 'The Holy Grail', 'What do you mean?']]

print(table(knights))
Output:
Sir Lancelot The Holy Grail Blue Sir Robin The Holy Grail I don't know that. Sir Galahad The Grail Blue. No, yel... King Arthur The Holy Grail What do you mean?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
python-tabulate to take the more lazy way than @ichabod801 Wink
>>> from tabulate import tabulate

>>> knights = [['Sir Lancelot', 'The Holy Grail', 'Blue'],
    ['Sir Robin', 'The Holy Grail', "I don't know that."],
    ['Sir Galahad', 'The Grail', 'Blue. No, yel...'],
    ['King Arthur', 'The Holy Grail', 'What do you mean?']]

>>> print(tabulate(knights))
------------  --------------  ------------------
Sir Lancelot  The Holy Grail  Blue
Sir Robin     The Holy Grail  I don't know that.
Sir Galahad   The Grail       Blue. No, yel...
King Arthur   The Holy Grail  What do you mean?
------------  --------------  ------------------

>>> print(tabulate(knights, tablefmt="pipe"))
|:-------------|:---------------|:-------------------|
| Sir Lancelot | The Holy Grail | Blue               |
| Sir Robin    | The Holy Grail | I don't know that. |
| Sir Galahad  | The Grail      | Blue. No, yel...   |
| King Arthur  | The Holy Grail | What do you mean?  |

>>> print(tabulate(knights, tablefmt="fancy_grid"))
╒══════════════╤════════════════╤════════════════════╕
│ Sir Lancelot │ The Holy Grail │ Blue               │
├──────────────┼────────────────┼────────────────────┤
│ Sir Robin    │ The Holy Grail │ I don't know that. │
├──────────────┼────────────────┼────────────────────┤
│ Sir Galahad  │ The Grail      │ Blue. No, yel...   │
├──────────────┼────────────────┼────────────────────┤
│ King Arthur  │ The Holy Grail │ What do you mean?  │
╘══════════════╧════════════════╧════════════════════╛
Reply
#5
(Jun-30-2018, 03:56 PM)ichabod801 Wrote: I've done this a few times. Get the string output as a list of lists. For each index in the sub-lists, get maximum width. Construct a format string with those widths and the justifications you want. Apply the rows to that format string repeatedly, printing or joining with '\n'.
BTDT2

just did one today which is why i was thinking or hoping something making this easy would be nice if it existed. you just print via a method of this class, giving each column as an argument. it would save the data and accumulate the max width of each column. once done a final method is called, then it actually prints the saved data, formatting each column to the max width or a specified width. it will also remember if a column is entirely numeric and right-justify if so, else left-justify, unless the justification is specified.

i didn't see any documentation for tabulate. where does that come from?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
Note that most terminals use a fixed width font so this will work just fine. For anyone who reads this in the future and is using a proportional font, this won't work. A small tkinter program to show the difference.
try:
       import Tkinter as tk     ## Python 2.x
except ImportError:
       import tkinter as tk     ## Python 3.x

class DifferentFonts():
    def __init__(self):
        self.top=tk.Tk()
        lit_1="Display lots of i's and l's-iiilll"
        lit_2="This is the same length as 1-wwwqq"
        ctr = 0
        for each_font in (('Verdana', 12),
                          ('DejaVuSansMono', 12)):
            tk.Label(self.top, text=each_font[0]+"-"*30,
                    font=each_font).grid(row=ctr)
            ctr += 1
            tk.Label(self.top, text=lit_1, font=each_font
                    ).grid(row=ctr, sticky="w")
            ctr += 1
            tk.Label(self.top, text=lit_2, font=each_font
                    ).grid(row=ctr, sticky="w")
            ctr += 1
            self.top.rowconfigure(ctr, weight=1, minsize=50)
            ctr += 1

        tk.Button(self.top, text="Quit", bg="orange",
               command=self.top.quit).grid(row=20)

        self.top.mainloop()

DifferentFonts() 
Reply
#7
(Jun-30-2018, 05:30 PM)Skaperen Wrote: i didn't see any documentation for tabulate. where does that come from?
It's in the in the link i posted,just scroll down.
Small project do not always make own document page,as this the one where documentation is on there Repo page or PyPi page.
Reply
#8
i missed that it was an "outside project" (my term). while i might use some on some of my projects, there are others i cannot use any for. this one is one where i can (i will be the only user). it is listing out backup partitions with columns like start sector and number of sectors.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python code to set column width 1418 11 1,145 Jan-20-2024, 07:20 AM
Last Post: Pedroski55
  Fixed colum width for rowLabels i Matplotlib pandabay 0 416 Jun-10-2023, 03:40 PM
Last Post: pandabay
  Setup host for consistent data transfer to client via TCP Gustav97 0 689 Jun-27-2022, 07:33 PM
Last Post: Gustav97
  Consistent error Led_Zeppelin 1 1,615 Dec-20-2021, 01:39 AM
Last Post: snippsat
  width of Unicode character Skaperen 6 2,690 Sep-27-2021, 12:41 AM
Last Post: Skaperen
  image.thumbnail(width, height) not working PCesarano 2 3,390 Apr-08-2021, 06:09 PM
Last Post: PCesarano
  Error printing colored text julio2000 0 1,484 Feb-02-2020, 07:04 PM
Last Post: julio2000
  How can I get the width of a string in Python? aquerci 14 16,084 May-27-2019, 06:00 PM
Last Post: heiner55
  fixed width numbers Skaperen 15 8,561 May-27-2019, 09:42 AM
Last Post: Skaperen
  Printing lines in a basic text file Drone4four 6 3,846 Aug-16-2018, 03:10 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020