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

Larry Bates larry.bates at websafe.com
Mon Oct 17 23:16:14 CEST 2005


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



More information about the Python-list mailing list