Structured writing to console, such as a table
Raaijmakers, Vincent (IndSys, GE Interlogix)
Vincent.Raaijmakers at ge.com
Tue Sep 2 02:12:35 CEST 2003
Agree, sometimes you need to reinvent the wheel. But this time I had the feeling that this way of presenting information
was something that was very well "thought out", done before even markup language was 'born'.
When looking at examples, modules etc I noticed indeed that data easily can be presented in PDF, HTML, LaTex format.
There is a lot of good python stuff available. Sometimes I can not believe it is all for free.
But such a simple thing like writing strings/data well structured to a console, who is doing that these days :-)
Thanks a lot for sharing your thoughts, but also all other people who shared their code and useful links!
From: Paul Moore [mailto:pf_moore at yahoo.co.uk]
Sent: Monday, September 01, 2003 6:46 PM
To: python-list at python.org
Subject: Re: Structured writing to console, such as a table
"Raaijmakers, Vincent (IndSys, GE Interlogix)" <Vincent.Raaijmakers at ge.com> writes:
> Ok, perhaps a question on a newbie level.
> I try to create a simple 'write to a console application' where all
> the items in a string do have a variable size:
> items = ["a", "bbbbbbbbb", "cc"]
> Well, actually, I need to print a table as simple text, nice lined
> up in a console. So:
> Item: Value: Another Value:
> a | 1 | 2
> bbbbbbbbb | 2 | 17
> cc | 3 | 5
> My hope was that somewhere in python land an existing module was
> waiting for me. A module that also prints lines, headers....
> Unfortunately, I can't find it. Books, Google...
> Before I reinvent this wheel... please give me some tips,
> references, examples...
This is going to sound unhelpful, but I suspect the problem is that
the problem is too simple - everyone *does* reinvent the wheel,
because it's faster than going to find a generic solution.
Having said that, I didn't manage to quickly write some code for you
The first question is, what does your data look like? From your
example, I'd say that you have a list of items
items = ["a", "bbbbbbbbb", "cc"]
but I'm not sure how you get your values.
Let's assume that in fact you have a "list of rows" type of
rows = [["a", 1, 2],
["bbbbbbbbb", 2, 17],
["cc", 3, 5]
This may or may not match your requirements, but you should be able to
either adapt my code or your data as needed.
The first problem is working out the column widths you need. That's
not difficult, just messy (because in some senses, the data is "the
wrong way round" - a list of coumns would be better for this step, but
worse later on).
def column_widths(titles, rows):
'''Calculate column widths for a "list of rows"'''
# Initialise widths to have all columns zero width to start with
widths = [len(str(title)) for title in titles]
# Adjust the width to allow space for each row in turn
for row in rows:
widths = [max(w, len(str(item)))
for w, item in zip(widths, row)]
That's a bit messy, so let's dissect it. I use list comprehensions a
lot here - if you don't know how they work, it's well worth studying
We start by setting widths to fit the titles. We assume that all rows
have the same number of elements - I don't check for this.
Then, for each row, we adjust the widths to fit the items in that row.
The list comprehension goes through each column, and the new width of
that column is either the old width (if the new item fits already) or
the length of the new item (if it is the biggest so far).
For each item, we're calculating len(str(item)) which is the length of
the string representation of the item - ie, the space required on
screen for that item.
OK, that was the ugly bit - now we just format the results. This is
simple, but a little long winded.
def format(titles, rows):
"Format a table"
# First calculate the column widths
widths = column_widths(titles, rows)
# Create the result as a list of lines - it's more flexible
# than printing directly
result = 
# Title line first (add 3 spaces between colums)
line = ' '.join([t.ljust(w) for t, w in zip(titles, widths)])
# Separator line (add -+- between columns)
line = '-+-'.join(['-' * w for w in widths])
# Rows of data
for row in rows:
line = ' | '.join([item.ljust(w) for item, w in zip(row, widths)])
Now you can do:
print "\n".join(format(titles, rows))
I hope this helps. As I say, the problem is often that matching the
algorithm to what you really want is harder than writing the code in
the first place, so don't be afraid to play with this (it's nearly
midnight, so I'm offering no guarantees that this code is correct :-))
This signature intentionally left blank
More information about the Python-list