Presenting Data neatly... how :)

Cliff Wells LogiplexSoftware at earthlink.net
Wed Nov 13 19:03:16 EST 2002


On Wed, 2002-11-13 at 15:15, e-tones.co.uk wrote:
> Hi all, im wring a command line python app that outputs 5 lines of data to
> the user in the forum
> 
> Name1                       votes              %votes
> 
> so, an example output might be
> 
> Dell    45    40%
> Microsoft   60    56%
> IBM   4     5%
> 
> Is there anyway to format it like this:
> 
> Dell            45       40%
> Microsoft   60       56%
> IBM             4      5%

Sounds suspiciously like homework, so here's what you should turn in:


votes = {'Dell': 45, 'Microsoft':60, 'IBM': 4}

columns = {
    'Company': max([len(n) for n in votes.keys() + ['Company']]),
    'Votes': 3,
    'Percent': 4
    }

format = "%%-%(Company)ss  %%%(Votes)sd  %%%(Percent)s.0f%%%%" % columns

totalVotes = float(reduce(int.__add__, votes.values()))

for company in votes:
    percent = votes[company] / totalVotes * 100
    print format % (company, votes[company], percent)
    


Be prepared to give a detailed explanation regarding how each line works
to your instructor ;)


-- 
Cliff Wells, Software Engineer
Logiplex Corporation (www.logiplex.net)
(503) 978-6726 x308  (800) 735-0555 x308
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 196 bytes
Desc: This is a digitally signed message part
URL: <http://mail.python.org/pipermail/python-list/attachments/20021113/0670540d/attachment.sig>


More information about the Python-list mailing list