469,631 Members | 970 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,631 developers. It's quick & easy.

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

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
1 2844
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.

Similar topics

38 posts views Thread by Anthony Baxter | last post: by
2 posts views Thread by nephish | last post: by
4 posts views Thread by Jody Gelowitz | last post: by
6 posts views Thread by Rex Eastbourne | last post: by
10 posts views Thread by Jeff B. | last post: by
9 posts views Thread by John Salerno | last post: by
2 posts views Thread by Sheldon | last post: by
1 post views Thread by epilogue | last post: by
8 posts views Thread by joemacbusiness | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.