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

Steven D'Aprano steve at REMOVETHIScyber.com.au
Mon Oct 17 09:12:02 EDT 2005


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.




More information about the Python-list mailing list