[newbie]Is there a module for print object in a readable format?

J

James Gan

I want the object printed in a readable format. For example,
x =[a, b, c, [d e]] will be printed as:
x--a
|_b
|_c
|___d
|_e

I tried pickled, marshel. They do different work. Is there another
module which do this kind of job?

Thanks!
James Gan
 
B

bruno modulix

James said:
I want the object printed in a readable format. For example,
x =[a, b, c, [d e]] will be printed as:
x--a
|_b
|_c
|___d
|_e

I tried pickled, marshel. They do different work.

Is there another
module which do this kind of job?

pprint
 
G

Guest

James> I want the object printed in a readable format. For

[...]

James> I tried pickled, marshel. They do different work. Is there
James> another module which do this kind of job?

from pprint import pprint
pprint(object)

bye,
e.
 
S

Steven D'Aprano

I want the object printed in a readable format. For example,
x =[a, b, c, [d e]] will be printed as:
x--a
|_b
|_c
|___d
|_e

I think you missed an "un-" in your first sentence.

:)

In general, if you want special/fancy/bizarre printing, you should either
write your own custom functions, or sub-class the objects in question.

E.g.

def multiline_print(L, indent=""):
"""Multi-line printing of lists.
WARNING: Untested and probably full of bugs.
"""
for item in L:
if type(item) == list:
multiline_print(item, indent + " ")
else:
print indent + "|_" + str(item)


Hope this helps.
 
S

Steven D'Aprano

James> I want the object printed in a readable format. For

[...]

James> I tried pickled, marshel. They do different work. Is there
James> another module which do this kind of job?

from pprint import pprint
pprint(object)

I don't think that even comes *close* to what James wants.

py> import pprint
py> pprint.pprint([1,2,3,4,[0,1,2], 5])
[1, 2, 3, 4, [0, 1, 2], 5]
 
L

Larry Bates

What if a is a tuple of lists? What if b is a
class? What if c is a file object? I can't even
tell what [d e] means? IMHO the output isn't in
"a readable format" at all (perhaps something got
lost in the posting).

pprint is as close are you are going to find for a
general solution to this problem.

-Larry Bates
I want the object printed in a readable format. For example,
x =[a, b, c, [d e]] will be printed as:
x--a
|_b
|_c
|___d
|_e


I think you missed an "un-" in your first sentence.

:)

In general, if you want special/fancy/bizarre printing, you should either
write your own custom functions, or sub-class the objects in question.

E.g.

def multiline_print(L, indent=""):
"""Multi-line printing of lists.
WARNING: Untested and probably full of bugs.
"""
for item in L:
if type(item) == list:
multiline_print(item, indent + " ")
else:
print indent + "|_" + str(item)


Hope this helps.
 
J

James Gan

Yes, that's what I need! Thank you all

bruno said:
James said:
I want the object printed in a readable format. For example,
x =[a, b, c, [d e]] will be printed as:
x--a
|_b
|_c
|___d
|_e

I tried pickled, marshel. They do different work.

Is there another
module which do this kind of job?


pprint
 
J

James Gan

Hi, Steven

:) width parameter do the magic :
>>> pprint.pprint([1,2,3,4,[0,1,2,[3,4]],5], width=1,indent=4)
[ 1,
2,
3,
4,
[ 0,
1,
2,
[ 3,
4]],
5]
"James" == James Gan <[email protected]> writes:

James> I want the object printed in a readable format. For

[...]

James> I tried pickled, marshel. They do different work. Is there
James> another module which do this kind of job?

from pprint import pprint
pprint(object)


I don't think that even comes *close* to what James wants.

py> import pprint
py> pprint.pprint([1,2,3,4,[0,1,2], 5])
[1, 2, 3, 4, [0, 1, 2], 5]
 
S

Steven D'Aprano

Hi, Steven

:) width parameter do the magic :
pprint.pprint([1,2,3,4,[0,1,2,[3,4]],5], width=1,indent=4)
[ 1,
2,
3,
4,
[ 0,
1,
2,
[ 3,
4]],
5]

That's not what I get. What are you using?

py> pprint.pprint([1,2,3,4,[0,1,2], 5], width=1, indent=4)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: pprint() got an unexpected keyword argument 'width'
 
D

dcrespo

Maybe you don't have an up to date python version. I'm using 2.4.2.
I tried:

import pprint
pprint.pprint([1,2,3,4,[0,1,2,[3,4]],5], width=1,indent=4)
[ 1,
2,
3,
4,
[ 0,
1,
2,
[ 3,
4]],
5]

Works fine.
 
M

Micah Elliott

That's not what I get. What are you using?

py> pprint.pprint([1,2,3,4,[0,1,2], 5], width=1, indent=4)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: pprint() got an unexpected keyword argument 'width'

I find it useful to have all relevant python versions (as listed on
http://www.python.org/download/) installed on my primary test
machines. It really helps me with portability testing:

$ ls /usr/local/bin/python*
/usr/local/bin/python
/usr/local/bin/python2.0
/usr/local/bin/python2.1
/usr/local/bin/python2.2
/usr/local/bin/python2.3
/usr/local/bin/python2.4

Then I see that v2.3 didn't have 'width':

$ python2.3 -c 'import pprint; pprint.pprint([1,2,3,4,[0,1,2], 5],
width=1, indent=4)'
Traceback (most recent call last):
File "<string>", line 1, in ?
TypeError: pprint() got an unexpected keyword argument 'width'

But v2.4 does:

$ python2.4 -c 'import pprint; pprint.pprint([1,2,3,4,[0,1,2], 5],
width=1, indent=4)'
[ 1,
2,
3,
4,
[ 0,
1,
2],
5]
 
K

Kent Johnson

Micah said:
That's not what I get. What are you using?

py> pprint.pprint([1,2,3,4,[0,1,2], 5], width=1, indent=4)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: pprint() got an unexpected keyword argument 'width'


I find it useful to have all relevant python versions (as listed on
http://www.python.org/download/) installed on my primary test
machines.

If you want to know when a feature was introduced, the docs are often helpful:

pprint( object[, stream[, indent[, width[, depth]]]])
<snip>
Changed in version 2.4: The parameters indent, width and depth were added.

Kent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top