
from StringIO import StringIO
from pprint import pprint as pp
left_justified = False
debug = False

textinfile = '''I$created$a$solution$for$a$program$I$am$writing$that
makes$columns$line$up$when$outputted$to$the$command
line.$I$am$new$to$Python,$and$am$hoping$I$might$get
some$input$on$this$topic.$In$the$text$file$that$the
program$reads,$the$fields$(columns)$are$delimited$by
'dollar'$and$the$records$(lines)$are$delimited$by
newlines$'\\n'.$So$one$line$looks$like:$'''

"""

Solution to problem posed at:
  http://www.kbrandt.com/2008/06/getting-command-line-output-columns-to.html
  
Output is the following if left-justified:

# Column-aligned output:
I        created a    solution for       a         program   I    am      writing that     
makes    columns line up       when      outputted to        the  command                  
line.    I       am   new      to        Python,   and       am   hoping  I       might get
some     input   on   this     topic.    In        the       text file    that    the      
program  reads,  the  fields   (columns) are       delimited by                            
'dollar' and     the  records  (lines)   are       delimited by                            
newlines '\n'.   So   one      line      looks     like:                                   

And like this if not left-justified:

# Column-aligned output:
       I created    a solution       for         a   program    I      am writing  that    
   makes columns line       up      when outputted        to  the command                  
   line.       I   am      new        to   Python,       and   am  hoping       I might get
    some   input   on     this    topic.        In       the text    file    that   the    
 program  reads,  the   fields (columns)       are delimited   by                          
'dollar'     and  the  records   (lines)       are delimited   by                          
newlines   '\n'.   So      one      line     looks     like:                               

"""

infile = StringIO(textinfile)

fieldsbyrecord= [line.strip().split('$') for line in infile]
if debug: print "fieldsbyrecord:"; print (fieldsbyrecord)
# pad to same number of fields per record
maxfields = max(len(record) for record in fieldsbyrecord)
fieldsbyrecord = [fields + ['']*(maxfields - len(fields))
                  for fields in fieldsbyrecord]
if debug: print "padded fieldsbyrecord:"; print (fieldsbyrecord)
# rotate
fieldsbycolumn = zip(*fieldsbyrecord)
if debug: print "fieldsbycolumn:"; print (fieldsbycolumn)
# calculate max fieldwidth per column
colwidths = [max(len(field) for field in column)
             for column in fieldsbycolumn]
if debug: print "colwidths:"; print (colwidths)
# pad fields in columns to colwidth with spaces
# (Use -width for left justification,)
fieldsbycolumn = [ ["%*s" % (-width if left_justified else width, field) for field in column]
                   for width, column in zip(colwidths, fieldsbycolumn) ]
if debug: print "padded fieldsbycolumn:"; print (fieldsbycolumn)
# rotate again
fieldsbyrecord = zip(*fieldsbycolumn)
if debug: print "padded rows and fields, fieldsbyrecord:"; print (fieldsbyrecord)
# printit
print "\n# Column-aligned output:"
for record in fieldsbyrecord:
  print " ".join(record)
  

