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

Discussion in 'Python' started by James Gan, Oct 17, 2005.

  1. James Gan

    James Gan Guest

    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
     
    James Gan, Oct 17, 2005
    #1
    1. Advertising

  2. James Gan wrote:
    > 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

    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in ''.split('@')])"
     
    bruno modulix, Oct 17, 2005
    #2
    1. Advertising

  3. James Gan

    Guest

    >>>>> "James" == James Gan <> 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)

    bye,
    e.

    --
    Enrico Sirola <>
     
    , Oct 17, 2005
    #3
  4. On Mon, 17 Oct 2005 17:25:35 +0800, James Gan wrote:

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



    --
    Steven.
     
    Steven D'Aprano, Oct 17, 2005
    #4
  5. On Mon, 17 Oct 2005 11:31:46 +0200, enrico.sirola_NOSPAM wrote:

    >>>>>> "James" == James Gan <> 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]


    --
    Steven.
     
    Steven D'Aprano, Oct 17, 2005
    #5
  6. James Gan

    Larry Bates Guest

    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

    Steven D'Aprano wrote:
    > On Mon, 17 Oct 2005 17:25:35 +0800, James Gan wrote:
    >
    >
    >>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.
    >
    >
    >
     
    Larry Bates, Oct 17, 2005
    #6
  7. James Gan

    James Gan Guest

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

    bruno modulix wrote:
    > James Gan wrote:
    >
    >>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
    >
     
    James Gan, Oct 19, 2005
    #7
  8. James Gan

    James Gan Guest

    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]
    >>>


    Steven D'Aprano wrote:
    > On Mon, 17 Oct 2005 11:31:46 +0200, enrico.sirola_NOSPAM wrote:
    >
    >
    >>>>>>>"James" == James Gan <> 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]
    >
    >
     
    James Gan, Oct 19, 2005
    #8
  9. On Wed, 19 Oct 2005 09:39:48 +0800, James Gan wrote:

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



    --
    Steven.
     
    Steven D'Aprano, Oct 19, 2005
    #9
  10. James Gan

    dcrespo Guest

    Re: Is there a module for print object in a readable format?

    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.
     
    dcrespo, Oct 19, 2005
    #10
  11. On Oct 20, Steven D'Aprano wrote:
    > 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]

    --
    _ _ ___
    |\/|icah |- lliott http://micah.elliott.name
    " " """
     
    Micah Elliott, Oct 19, 2005
    #11
  12. James Gan

    Kent Johnson Guest

    Micah Elliott wrote:
    > On Oct 20, Steven D'Aprano wrote:
    >
    >>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
     
    Kent Johnson, Oct 19, 2005
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Stiles

    Portable readable dump format

    Chris Stiles, Oct 26, 2003, in forum: Python
    Replies:
    3
    Views:
    517
    David Mertz
    Oct 26, 2003
  2. reuven
    Replies:
    1
    Views:
    1,457
    Mike Treseler
    Jul 27, 2006
  3. Satish

    Julian timestamp to readable format

    Satish, Oct 5, 2004, in forum: C Programming
    Replies:
    2
    Views:
    2,858
    Satish
    Oct 11, 2004
  4. Replies:
    2
    Views:
    302
    santosh
    Nov 30, 2006
  5. Ulf Meinhardt
    Replies:
    0
    Views:
    494
    Ulf Meinhardt
    Sep 13, 2009
Loading...

Share This Page