align on char

Mike C. Fletcher mcfletch at rogers.com
Sat Jun 19 06:12:41 CEST 2004


 >>> lines = (  'user1 : John Hunter',  'another user : Bill Bragg',  
'yet on more : Hi There', )
 >>> lines
('user1 : John Hunter', 'another user : Bill Bragg', 'yet on more : Hi 
There')
 >>> split = [ line.split( ':',1) for line in lines ]
 >>> split
[['user1 ', ' John Hunter'], ['another user ', ' Bill Bragg'], ['yet on 
more ', ' Hi There']]
 >>> maxLength = max([len(line[0]) for line in split ])
 >>> maxLength
13
 >>> aligned = [ '%s:%s'%(prefix.ljust(maxLength),suffix) for 
(prefix,suffix) in split ]
 >>> aligned
['user1        : John Hunter', 'another user : Bill Bragg', 'yet on 
more  : Hi There']
 >>> for line in aligned:
...     print line
...
user1        : John Hunter
another user : Bill Bragg
yet on more  : Hi There
 >>>

HTH,
Mike

John Hunter wrote:

>Suppose you have a sequence of strings, all known to have at least one
>occurance of char
>
>lines = (
>  'user1 : John Hunter',
>  'another user : Bill Bragg',
>  'yet on more : Hi There', 
>)
>
>what is the best / easiest / most elegant way of producing this?
>
>aligned = (
>  'user1        : John Hunter',
>  'another user : Bill Bragg',
>  'yet on more  : Hi There', 
>)
>
>Thanks!
>JDH
>  
>
 
________________________________________________
  Mike C. Fletcher
  Designer, VR Plumber, Coder
  http://members.rogers.com/mcfletch/
  blog: http://zope.vex.net/~mcfletch/plumbing/





More information about the Python-list mailing list