Pretty print Python Lists -
i have written cli utility in python , have list 1:m list-items of paths like:
i want display list in more readable manner
like:
[ (1, '/path/here'), ('path/here/again'), (2, 'a/different/path/here'), ('another/path/here') ]
or in table-like
format (e.g.):
1 /path/here path/here/again 2 a/different/path/here another/path/here
note, list have 20 or more list-items.
thanks!
to start:
>>> mylist = [ ... (1, '/path/here', 'path/here/again'), ... (2, 'a/different/path/here', 'another/path/here') ... ]
play join()
, map()
, , str()
, along python 3's nice print()
function:
>>> print(*('\t'.join(map(str, item)) item in mylist), sep='\n') 1 /path/here path/here/again 2 a/different/path/here another/path/here
or try string formatting instead of join()
, map()
:
>>> print(*(str(col) + '\t' + (len(item)*'{}').format(*(i.ljust(25) in item)) col,*item in mylist), sep='\n') 1 /path/here path/here/again 2 a/different/path/here another/path/here
you pprint
module.
Comments
Post a Comment