<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Getting Command Line Columns to Line up with Python</title>
	<atom:link href="http://www.kbrandt.com/2008/06/getting-command-line-output-columns-to.html/feed" rel="self" type="application/rss+xml" />
	<link>http://www.kbrandt.com/2008/06/getting-command-line-output-columns-to.html</link>
	<description>Original computing articles by a systems administrator</description>
	<lastBuildDate>Wed, 07 Sep 2011 00:44:02 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Paddy3118</title>
		<link>http://www.kbrandt.com/2008/06/getting-command-line-output-columns-to.html/comment-page-1#comment-14</link>
		<dc:creator>Paddy3118</dc:creator>
		<pubDate>Wed, 04 Jun 2008 21:01:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.kbrandt.com/?p=24#comment-14</guid>
		<description>Hi,&lt;br/&gt;Nice problem.&lt;br/&gt;I have this solution in Python:&lt;br/&gt;&lt;br/&gt;from StringIO import StringIO&lt;br/&gt;from pprint import pprint as pp&lt;br/&gt;left_justified = False&lt;br/&gt;debug = False&lt;br/&gt;&lt;br/&gt;textinfile = &#039;&#039;&#039;I$created$a$solution$for$a$program$I$am$writing$that&lt;br/&gt;makes$columns$line$up$when$outputted$to$the$command&lt;br/&gt;line.$I$am$new$to$Python,$and$am$hoping$I$might$get&lt;br/&gt;some$input$on$this$topic.$In$the$text$file$that$the&lt;br/&gt;program$reads,$the$fields$(columns)$are$delimited$by&lt;br/&gt;&#039;dollar&#039;$and$the$records$(lines)$are$delimited$by&lt;br/&gt;newlines$&#039;\\n&#039;.$So$one$line$looks$like:$&#039;&#039;&#039;&lt;br/&gt;&lt;br/&gt;&quot;&quot;&quot;&lt;br/&gt;&lt;br/&gt;Solution to problem posed at:&lt;br/&gt;&#160;&#160;http://www.kbrandt.com/2008/06/getting-command-line-output-columns-to.html&lt;br/&gt;&lt;br/&gt;Output is the following if left-justified:&lt;br/&gt;&lt;br/&gt;# Column-aligned output:&lt;br/&gt;I        created a    solution for       a         program   I    am      writing that&lt;br/&gt;makes    columns line up       when      outputted to        the  command&lt;br/&gt;line.    I       am   new      to        Python,   and       am   hoping  I       might get&lt;br/&gt;some     input   on   this     topic.    In        the       text file    that    the&lt;br/&gt;program  reads,  the  fields   (columns) are       delimited by&lt;br/&gt;&#039;dollar&#039; and     the  records  (lines)   are       delimited by&lt;br/&gt;newlines &#039;\n&#039;.   So   one      line      looks     like:&lt;br/&gt;&lt;br/&gt;And like this if not left-justified:&lt;br/&gt;&lt;br/&gt;# Column-aligned output:&lt;br/&gt;&#160;&#160;&#160;&#160;&#160;&#160;&#160;I created    a solution       for         a   program    I      am writing  that&lt;br/&gt;&#160;&#160;&#160;makes columns line       up      when outputted        to  the command&lt;br/&gt;&#160;&#160;&#160;line.       I   am      new        to   Python,       and   am  hoping       I might get&lt;br/&gt;&#160;&#160;&#160;&#160;some   input   on     this    topic.        In       the text    file    that   the&lt;br/&gt;&#160;program  reads,  the   fields (columns)       are delimited   by&lt;br/&gt;&#039;dollar&#039;     and  the  records   (lines)       are delimited   by&lt;br/&gt;newlines   &#039;\n&#039;.   So      one      line     looks     like:&lt;br/&gt;&lt;br/&gt;&quot;&quot;&quot;&lt;br/&gt;&lt;br/&gt;infile = StringIO(textinfile)&lt;br/&gt;&lt;br/&gt;fieldsbyrecord= [line.strip().split(&#039;$&#039;) for line in infile]&lt;br/&gt;if debug: print &quot;fieldsbyrecord:&quot;; print (fieldsbyrecord)&lt;br/&gt;# pad to same number of fields per record&lt;br/&gt;maxfields = max(len(record) for record in fieldsbyrecord)&lt;br/&gt;fieldsbyrecord = [fields + [&#039;&#039;]*(maxfields - len(fields))&lt;br/&gt;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;for fields in fieldsbyrecord]&lt;br/&gt;if debug: print &quot;padded fieldsbyrecord:&quot;; print (fieldsbyrecord)&lt;br/&gt;# rotate&lt;br/&gt;fieldsbycolumn = zip(*fieldsbyrecord)&lt;br/&gt;if debug: print &quot;fieldsbycolumn:&quot;; print (fieldsbycolumn)&lt;br/&gt;# calculate max fieldwidth per column&lt;br/&gt;colwidths = [max(len(field) for field in column)&lt;br/&gt;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;for column in fieldsbycolumn]&lt;br/&gt;if debug: print &quot;colwidths:&quot;; print (colwidths)&lt;br/&gt;# pad fields in columns to colwidth with spaces&lt;br/&gt;# (Use -width for left justification,)&lt;br/&gt;fieldsbycolumn = [ [&quot;%*s&quot; % (-width if left_justified else width, field) for field in column]&lt;br/&gt;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;for width, column in zip(colwidths, fieldsbycolumn) ]&lt;br/&gt;if debug: print &quot;padded fieldsbycolumn:&quot;; print (fieldsbycolumn)&lt;br/&gt;# rotate again&lt;br/&gt;fieldsbyrecord = zip(*fieldsbycolumn)&lt;br/&gt;if debug: print &quot;padded rows and fields, fieldsbyrecord:&quot;; print (fieldsbyrecord)&lt;br/&gt;# printit&lt;br/&gt;print &quot;\n# Column-aligned output:&quot;&lt;br/&gt;for record in fieldsbyrecord:&lt;br/&gt;&#160;&#160;print &quot; &quot;.join(record)&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;- Paddy.</description>
		<content:encoded><![CDATA[<p>Hi,<br />Nice problem.<br />I have this solution in Python:</p>
<p>from StringIO import StringIO<br />from pprint import pprint as pp<br />left_justified = False<br />debug = False</p>
<p>textinfile = &#8221;&#8217;I$created$a$solution$for$a$program$I$am$writing$that<br />makes$columns$line$up$when$outputted$to$the$command<br />line.$I$am$new$to$Python,$and$am$hoping$I$might$get<br />some$input$on$this$topic.$In$the$text$file$that$the<br />program$reads,$the$fields$(columns)$are$delimited$by<br />&#8216;dollar&#8217;$and$the$records$(lines)$are$delimited$by<br />newlines$&#8217;\\n&#8217;.$So$one$line$looks$like:$&#8221;&#8217;</p>
<p>&#8220;&#8221;"</p>
<p>Solution to problem posed at:<br />&nbsp;&nbsp;<a href="http://www.kbrandt.com/2008/06/getting-command-line-output-columns-to.html"  rel="nofollow">http://www.kbrandt.com/2008/06/getting-command-line-output-columns-to.html</a></p>
<p>Output is the following if left-justified:</p>
<p># Column-aligned output:<br />I        created a    solution for       a         program   I    am      writing that<br />makes    columns line up       when      outputted to        the  command<br />line.    I       am   new      to        Python,   and       am   hoping  I       might get<br />some     input   on   this     topic.    In        the       text file    that    the<br />program  reads,  the  fields   (columns) are       delimited by<br />&#8216;dollar&#8217; and     the  records  (lines)   are       delimited by<br />newlines &#8216;\n&#8217;.   So   one      line      looks     like:</p>
<p>And like this if not left-justified:</p>
<p># Column-aligned output:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;I created    a solution       for         a   program    I      am writing  that<br />&nbsp;&nbsp;&nbsp;makes columns line       up      when outputted        to  the command<br />&nbsp;&nbsp;&nbsp;line.       I   am      new        to   Python,       and   am  hoping       I might get<br />&nbsp;&nbsp;&nbsp;&nbsp;some   input   on     this    topic.        In       the text    file    that   the<br />&nbsp;program  reads,  the   fields (columns)       are delimited   by<br />&#8216;dollar&#8217;     and  the  records   (lines)       are delimited   by<br />newlines   &#8216;\n&#8217;.   So      one      line     looks     like:</p>
<p>&#8220;&#8221;"</p>
<p>infile = StringIO(textinfile)</p>
<p>fieldsbyrecord= [line.strip().split('$') for line in infile]<br />if debug: print &#8220;fieldsbyrecord:&#8221;; print (fieldsbyrecord)<br /># pad to same number of fields per record<br />maxfields = max(len(record) for record in fieldsbyrecord)<br />fieldsbyrecord = [fields + ['']*(maxfields &#8211; len(fields))<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for fields in fieldsbyrecord]<br />if debug: print &#8220;padded fieldsbyrecord:&#8221;; print (fieldsbyrecord)<br /># rotate<br />fieldsbycolumn = zip(*fieldsbyrecord)<br />if debug: print &#8220;fieldsbycolumn:&#8221;; print (fieldsbycolumn)<br /># calculate max fieldwidth per column<br />colwidths = [max(len(field) for field in column)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for column in fieldsbycolumn]<br />if debug: print &#8220;colwidths:&#8221;; print (colwidths)<br /># pad fields in columns to colwidth with spaces<br /># (Use -width for left justification,)<br />fieldsbycolumn = [ ["%*s" % (-width if left_justified else width, field) for field in column]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for width, column in zip(colwidths, fieldsbycolumn) ]<br />if debug: print &#8220;padded fieldsbycolumn:&#8221;; print (fieldsbycolumn)<br /># rotate again<br />fieldsbyrecord = zip(*fieldsbycolumn)<br />if debug: print &#8220;padded rows and fields, fieldsbyrecord:&#8221;; print (fieldsbyrecord)<br /># printit<br />print &#8220;\n# Column-aligned output:&#8221;<br />for record in fieldsbyrecord:<br />&nbsp;&nbsp;print &#8221; &#8220;.join(record)</p>
<p>- Paddy.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

