By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,525 Members | 1,689 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,525 IT Pros & Developers. It's quick & easy.

Simple - Pretty Printing (pprint) a list with a max width?

P: n/a
Hi all,

Perhaps it's not supposed to work like this but I thought if you
supplied a width to pprint it would nicely format a list to the
width.

KEYS = ['search_keys', 'Section', 'site', 'Employee', 'JobClassCode',
'XBoss', 'Department',
'LocationDesc', 'cn', 'Division', 'Fax', 'SectionCode',
'last_update', 'location',
'mail', 'Nickname', 'JobClass', 'username', 'Name',
'Shift', 'FunctionalArea', 'p4user',
'euid', 'Position', 'DivisionCode', 'OfficePhone',
'CellPhone', 'Supervisor', 'aim',
'url', 'aim_id', 'Assistant', 'DeptNo', 'OtherPhone',
'LocationCode']

pp = pprint.PrettyPrinter(indent=2)
pp.pprint(KEYS)
[ 'search_keys',
'Section',
Nov 7 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Nov 7, 9:45*am, rh0dium <steven.kl...@gmail.comwrote:
Hi all,

Perhaps it's not supposed to work like this but I thought if you
supplied a width to pprint it would nicely format a list to the
width.

KEYS = ['search_keys', 'Section', 'site', 'Employee', 'JobClassCode',
'XBoss', 'Department',
* * * * * * 'LocationDesc', *'cn', 'Division', 'Fax', 'SectionCode',
'last_update', *'location',
* * * * * * 'mail', 'Nickname', 'JobClass', 'username', 'Name',
'Shift', 'FunctionalArea', 'p4user',
* * * * * * 'euid', 'Position', *'DivisionCode', 'OfficePhone',
'CellPhone', 'Supervisor', 'aim',
* * * * * * 'url', 'aim_id', 'Assistant', 'DeptNo', 'OtherPhone',
'LocationCode']

*pp = pprint.PrettyPrinter(indent=2)
*pp.pprint(KEYS)
* [ 'search_keys',
* * 'Section',
* .
* .
That works but it doesn't span the width if you put a width - for that
matter I can't seem to get width to do anything. *Anyone else see
that? * What I want is to be able to given a list provide a nice fancy
evenly spaced columnar output.

* search_keys * * * *Section * * * *site * * * * *Employee
* JobClassCode * * * XBoss * * * * *Department * *LocationDesc
* cn * * * * * * * * Division * * * Fax * * * * * SectionCode

It's clear pprint does not do this - Does anyone have something for
working with lists to pretty format them?
Try this:

data = [
"this is a really long entry, longer than will fit in a single
column",
"this is another one, boy this guy really talks a lot!",
"and in conclusion, I find that more is more, except when it's not"
]

import textwrap
from itertools import izip,chain,repeat

for linetuple in izip(map(None,*[textwrap.wrap(d,15)
for d in data])):
linetuple = ("" if c is None else c for c in linetuple[0])
print "|%-15s|%-15s|%-15s|" % tuple(linetuple)

print
colwidths = [16,15,12]
fmtstring = "|" + "|".join("%%-%ds" % w for w in colwidths) + "|"
for linetuple in izip(map(None,*[textwrap.wrap(d,colwidths[i])
for i,d in enumerate(data)])):
linetuple = ("" if c is None else c for c in linetuple[0])
print fmtstring % tuple(linetuple)

Prints:

|this is a |this is another|and in |
|really long |one, boy this |conclusion, I |
|entry, longer |guy really |find that more |
|than will fit |talks a lot! |is more, except|
|in a single | |when it's not |
|column | | |

|this is a really|this is another|and in |
|long entry, |one, boy this |conclusion, |
|longer than will|guy really |I find that |
|fit in a single |talks a lot! |more is |
|column | |more, except|
| | |when it's |
| | |not |

-- Paul
Nov 7 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.