How do I print parameters of multiple objects in table form? [duplicate]
I have a list of objects and I want to print the parameters of each in a nice table.
My code is here:
class item(object):
def __init__(self, thing, owner, color):
self.thing = thing
self.owner = owner
self.color = color
bin = []
bin.append(item('shirt', 'John', 'red'))
bin.append(item('skirt', 'Jane', 'blue'))
## Need help here
## Can't figure this out
print '%-10s %-10s %-10s' % (bin[0].thing, bin[0].owner, bin[0].color)
The output I am trying to get is
shirt skirt
John Jane
red blue
Solution 1:
Here's something I found long ago that's been very useful and still works. I finally got around recently to bringing it up to date with most of the latest Py 2.7.x and 3.x standards, although most of the changes were what I would call cosmetic and/or fairly trivial.
# File: textformatter.py
# Author: Hamish B Lawson
# Date: 19 Nov 1999
# from http://www.faqts.com/knowledge_base/view.phtml/aid/4517
# Viewable via Internet Archive WayBackMachine:
# https://web.archive.org/web/20080709071216/http://www.faqts.com/knowledge_base/view.phtml/aid/4517
"""
Here is TextFormatter, a simple module for formatting text into
columns of specified widths. It does multiline wrapping and supports
LEFT, CENTER (or CENTRE), and RIGHT alignment.
Revisions:
SKWM Made filling & padding optional, tweaked some edge cases.
MRM Removed import of deprecated string module, made all classes
explicitly new style, capitalized public constants, updated
documentation. Tweaked test() function.
MRM Modified to work in both Python 2.7 and 3.x.
"""
from __future__ import print_function
LEFT, CENTER, RIGHT = range(3)
CENTRE = CENTER
class TextFormatter(object):
"""
Formats text into columns.
Constructor takes a list of dictionaries that each specify the
properties for a column. Dictionary entries can be:
'width' : the width within which the text will be wrapped
'alignment' : LEFT | CENTER | RIGHT
'margin' : number of space characters to prefix in front of column
The compose() method takes a list of strings and returns a formatted
string consisting of each string wrapped within its respective column.
Example:
import textformatter
formatter = textformatter.TextFormatter(
(
{'width': 10},
{'width': 12, 'margin': 4},
{'width': 20, 'margin': 8, 'alignment': textformatter.RIGHT},
)
)
print(formatter.compose(
(
"A rather short paragraph",
"Here is a paragraph containing a veryveryverylongwordindeed.",
"And now for something on the right-hand side.",
)
))
gives:
A rather Here is a And now for
short paragraph something on the
paragraph containing a right-hand side.
veryveryvery
longwordinde
ed.
"""
class Column(object):
def __init__(self, width=75, alignment=LEFT, margin=0, fill=1, pad=1):
self.width = width
self.alignment = alignment
self.margin = margin
self.fill = fill
self.pad = pad
self.lines = []
def align(self, line):
if self.alignment == CENTER:
return line.center(self.width)
elif self.alignment == RIGHT:
return line.rjust(self.width)
else:
if self.pad:
return line.ljust(self.width)
else:
return line
def wrap(self, text):
self.lines = []
words = []
if self.fill: # SKWM
for word in text.split():
wordlen = len(word)
if wordlen <= self.width: # fixed MRM
words.append(word)
else:
for i in range(0, wordlen, self.width):
words.append(word[i:i+self.width])
else:
for line in text.split('\n'):
for word in line.split():
for i in range(0, len(word), self.width):
words.append(word[i:i+self.width])
words.append('\n')
if words[-1] == '\n': words.pop() # remove trailing newline
if words:
current = words.pop(0)
for word in words:
increment = 1 + len(word)
if word == '\n':
self.lines.append(self.align(current))
current = ''
elif len(current) + increment > self.width:
self.lines.append(self.align(current))
current = word
else:
if current:
current = current + ' ' + word
else:
current = word
if current: self.lines.append(self.align(current))
def getline(self, index):
if index < len(self.lines):
return ' '*self.margin + self.lines[index]
else:
if self.pad:
return ' ' * (self.margin + self.width)
else:
return ''
def numlines(self):
return len(self.lines)
def __init__(self, colspeclist):
self.columns = []
for colspec in colspeclist:
self.columns.append(TextFormatter.Column(**colspec))
def compose(self, textlist):
numlines = 0
textlist = list(textlist)
if len(textlist) != len(self.columns):
raise IndexError("Number of text items does not match columns")
for text, column in zip(textlist, self.columns):
column.wrap(text)
numlines = max(numlines, column.numlines())
complines = [''] * numlines
for ln in range(numlines):
for column in self.columns:
complines[ln] = complines[ln] + column.getline(ln)
return '\n'.join(complines) + '\n'
def test():
import textformatter
formatter = textformatter.TextFormatter(
(
{'width': 10},
{'width': 12, 'margin': 4, 'fill': 0},
{'width': 20, 'margin': 8, 'alignment': textformatter.RIGHT},
)
)
print(formatter.compose(
(
"A rather short paragraph",
"Here is\na paragraph containing a veryveryverylongwordindeed.",
"And now for something\non the RIGHT-hand side.",
)
)
)
__all__ = [TextFormatter, LEFT, CENTER, CENTRE, RIGHT]
if __name__ == '__main__':
test()
Solution 2:
for attr in ('thing', 'owner', 'color'):
for item in bin:
print '%-10s'%getattr(item, attr),
print
It's more compact to use a list comprehension
for atts in ('thing', 'owner', 'color'):
print ' '.join('%-10s'%getattr(item, attr) for item in bin)