472,358 Members | 1,770 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,358 software developers and data experts.

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

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',

pp = pprint.PrettyPrinter(indent=2)
[ 'search_keys',
Nov 7 '08 #1
1 3051
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

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',

*pp = pprint.PrettyPrinter(indent=2)
* [ '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
"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)

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)


|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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: Anthony Baxter | last post by:
On behalf of the Python development team and the Python community, I'm happy to announce the release of Python 2.3.1 (final). Python 2.3.1 is a pure bug fix release of Python 2.3, released in...
by: nephish | last post by:
Hey there, i have been spending some time learning python. i do enjoy it so. Heres the deal. i need some dirrection advice. i have a friend that wants me to set up a customer database for him....
by: Jody Gelowitz | last post by:
I am having a problem with printing selected pages. Actually, the problem isn't with printing selected pages as it is more to do with having blank pages print for those pages that have not been...
by: Rex Eastbourne | last post by:
Hi all, I've written the following simple macro called debug(aname, avalue) that prints out the name of an expression and its value: def debug(aname, avalue): print aname, 'is':...
by: Jeff B. | last post by:
Has anyone come across a decent algorithm for implementing word wrap features in .net printing? I have a small component that uses basic printing techniques (i.e. e.Graphics.DrawString in a...
by: John Salerno | last post by:
Just wondering if this will ever happen, maybe in 3.0 when print becomes a function too? It would be a nice option to have it available without importing it every time, but maybe making it a...
by: Sheldon | last post by:
Good day, I would like to know if there is a way to print all the variables set in a python program with having to write "print variable" on all? sincerely, Sheldon
by: epilogue | last post by:
Hey guys Im pretty new to Java and while I am finding it enjoyable i am getting several errors!!! Do you think you could help me on this particular question of an Assignment im doing. I have to...
by: joemacbusiness | last post by:
Hi All, How do I format printed data in python? I could not find this in the Python Reference Manual: http://docs.python.org/ref/print.html Nor could I find it in Matloff's great tutorial:...
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.