<?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: Pretty-printing a table in Python</title>
	<atom:link href="http://ginstrom.com/scribbles/2007/09/04/pretty-printing-a-table-in-python/feed/" rel="self" type="application/rss+xml" />
	<link>http://ginstrom.com/scribbles/2007/09/04/pretty-printing-a-table-in-python/</link>
	<description>Random scribbling about programming, translation, and Japan</description>
	<lastBuildDate>Thu, 10 May 2012 05:41:14 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
	<item>
		<title>By: Alex</title>
		<link>http://ginstrom.com/scribbles/2007/09/04/pretty-printing-a-table-in-python/comment-page-1/#comment-95639</link>
		<dc:creator>Alex</dc:creator>
		<pubDate>Sat, 30 Jul 2011 18:23:13 +0000</pubDate>
		<guid isPermaLink="false">http://www.ginstrom.com/scribbles/2007/09/04/pretty-printing-a-table-in-python/#comment-95639</guid>
		<description>Ryan,
What should I do to use float values on the table? I&#039;ve been working at that for a while and can&#039;t figure it out...</description>
		<content:encoded><![CDATA[<p>Ryan,<br />
What should I do to use float values on the table? I&#8217;ve been working at that for a while and can&#8217;t figure it out&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ryan Ginstrom</title>
		<link>http://ginstrom.com/scribbles/2007/09/04/pretty-printing-a-table-in-python/comment-page-1/#comment-63723</link>
		<dc:creator>Ryan Ginstrom</dc:creator>
		<pubDate>Wed, 30 Mar 2011 02:37:32 +0000</pubDate>
		<guid isPermaLink="false">http://www.ginstrom.com/scribbles/2007/09/04/pretty-printing-a-table-in-python/#comment-63723</guid>
		<description>@J_Tom_Moon_79

Thanks for the code! I edited your comment to wrap the code in pre tags rather than code tags. I need to update the css file so that multiline code looks nice.</description>
		<content:encoded><![CDATA[<p>@J_Tom_Moon_79</p>
<p>Thanks for the code! I edited your comment to wrap the code in pre tags rather than code tags. I need to update the css file so that multiline code looks nice.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: J_Tom_Moon_79</title>
		<link>http://ginstrom.com/scribbles/2007/09/04/pretty-printing-a-table-in-python/comment-page-1/#comment-62454</link>
		<dc:creator>J_Tom_Moon_79</dc:creator>
		<pubDate>Wed, 23 Mar 2011 23:38:16 +0000</pubDate>
		<guid isPermaLink="false">http://www.ginstrom.com/scribbles/2007/09/04/pretty-printing-a-table-in-python/#comment-62454</guid>
		<description>doh! 
This forum software removed my formatting and indentation!
my post merely a minor re-write, anyway.</description>
		<content:encoded><![CDATA[<p>doh!<br />
This forum software removed my formatting and indentation!<br />
my post merely a minor re-write, anyway.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: J_Tom_Moon_79</title>
		<link>http://ginstrom.com/scribbles/2007/09/04/pretty-printing-a-table-in-python/comment-page-1/#comment-62453</link>
		<dc:creator>J_Tom_Moon_79</dc:creator>
		<pubDate>Wed, 23 Mar 2011 23:36:10 +0000</pubDate>
		<guid isPermaLink="false">http://www.ginstrom.com/scribbles/2007/09/04/pretty-printing-a-table-in-python/#comment-62453</guid>
		<description>Hi, Here&#039;s a minor update for python 3.2.  
+ Uses character &quot;&#124;&quot; to delineate the table lines.
- Removes number formatting.
&lt;pre lang=&quot;python&quot;&gt;
def pprintTable(out, table):
	&quot;&quot;&quot;Prints out a table of data, padded for alignment
	@param out: Output stream (file-like object)
	@param table: The table to print. A list of lists.
	Each row must have the same number of columns. &quot;&quot;&quot;
	
	def format(num):
		&quot;&quot;&quot;Format a number according to given places.  Adds commas, etc. Will truncate floats into ints!&quot;&quot;&quot;
		#try:
		#	inum = int(num)
		#	return locale.format(&quot;%.*f&quot;, (0, inum), True)
		#except (ValueError, TypeError):
		return str(num)
	def get_max_width(table1, index1):
		&quot;&quot;&quot;Get the maximum width of the given column index&quot;&quot;&quot;
		return max([len(format(row1[index1])) for row1 in table1])
	
	col_paddings = []
	for i in range(len(table[0])):
		col_paddings.append(get_max_width(table, i))
	
	for row in table:
		# left col
		print(row[0].ljust(col_paddings[0] + 1),end=&quot;&#124;&#124;&quot;,file=out)
		# rest of the cols
		for i in range(1, len(row)):
			col = format(row[i]).rjust(col_paddings[i] + 1)
			print(col,end=&quot; &#124;&quot;,file=out)
		print(file=out)
	return
&lt;/pre&gt;

Example use:
&lt;pre lang=&quot;python&quot;&gt;
$ t = [
		[&quot;Header&quot;,&quot;A&quot;,&quot;B&quot;,&quot;C&quot;],
		[&quot;Default Values&quot;,&quot;3&quot;,&quot;65&quot;,&quot;100&quot;],
		[&quot;Actual Values&quot;,&quot;432&quot;,&quot;22222&quot;,&quot;99&quot;]
	]
$ pprintTable(sys.stdout,t)
Header         &#124;&#124;   A &#124;     B &#124;   C &#124;
Default Values &#124;&#124;   3 &#124;    65 &#124; 100 &#124;
Actual Values  &#124;&#124; 432 &#124; 22222 &#124;  99 &#124;
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Hi, Here&#8217;s a minor update for python 3.2.<br />
+ Uses character &#8220;|&#8221; to delineate the table lines.<br />
- Removes number formatting.</p>
<pre lang="python">
def pprintTable(out, table):
	"""Prints out a table of data, padded for alignment
	@param out: Output stream (file-like object)
	@param table: The table to print. A list of lists.
	Each row must have the same number of columns. """

	def format(num):
		"""Format a number according to given places.  Adds commas, etc. Will truncate floats into ints!"""
		#try:
		#	inum = int(num)
		#	return locale.format("%.*f", (0, inum), True)
		#except (ValueError, TypeError):
		return str(num)
	def get_max_width(table1, index1):
		"""Get the maximum width of the given column index"""
		return max([len(format(row1[index1])) for row1 in table1])

	col_paddings = []
	for i in range(len(table[0])):
		col_paddings.append(get_max_width(table, i))

	for row in table:
		# left col
		print(row[0].ljust(col_paddings[0] + 1),end="||",file=out)
		# rest of the cols
		for i in range(1, len(row)):
			col = format(row[i]).rjust(col_paddings[i] + 1)
			print(col,end=" |",file=out)
		print(file=out)
	return
</pre>
<p>Example use:</p>
<pre lang="python">
$ t = [
		["Header","A","B","C"],
		["Default Values","3","65","100"],
		["Actual Values","432","22222","99"]
	]
$ pprintTable(sys.stdout,t)
Header         ||   A |     B |   C |
Default Values ||   3 |    65 | 100 |
Actual Values  || 432 | 22222 |  99 |
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Konrad</title>
		<link>http://ginstrom.com/scribbles/2007/09/04/pretty-printing-a-table-in-python/comment-page-1/#comment-15016</link>
		<dc:creator>Konrad</dc:creator>
		<pubDate>Sun, 14 Feb 2010 18:40:36 +0000</pubDate>
		<guid isPermaLink="false">http://www.ginstrom.com/scribbles/2007/09/04/pretty-printing-a-table-in-python/#comment-15016</guid>
		<description>Or try the texttable package from PyPI:
http://pypi.python.org/pypi?name=texttable&amp;:action=display</description>
		<content:encoded><![CDATA[<p>Or try the texttable package from PyPI:<br />
<a href="http://pypi.python.org/pypi?name=texttable&#038;amp">http://pypi.python.org/pypi?name=texttable&#038;amp</a>;:action=display</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ryan Ginstrom</title>
		<link>http://ginstrom.com/scribbles/2007/09/04/pretty-printing-a-table-in-python/comment-page-1/#comment-15</link>
		<dc:creator>Ryan Ginstrom</dc:creator>
		<pubDate>Thu, 03 Jul 2008 13:52:41 +0000</pubDate>
		<guid isPermaLink="false">http://www.ginstrom.com/scribbles/2007/09/04/pretty-printing-a-table-in-python/#comment-15</guid>
		<description>@Robert
There&#039;s &lt;a href=&quot;http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/267662&quot;&gt;a recipe on activestate.com&lt;/a&gt; that does this.</description>
		<content:encoded><![CDATA[<p>@Robert<br />
There&#8217;s <a href="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/267662">a recipe on activestate.com</a> that does this.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert</title>
		<link>http://ginstrom.com/scribbles/2007/09/04/pretty-printing-a-table-in-python/comment-page-1/#comment-14</link>
		<dc:creator>Robert</dc:creator>
		<pubDate>Thu, 03 Jul 2008 13:17:05 +0000</pubDate>
		<guid isPermaLink="false">http://www.ginstrom.com/scribbles/2007/09/04/pretty-printing-a-table-in-python/#comment-14</guid>
		<description>Another thing (and maybe Python has a module that does this) is to print pretty tables:

+----------------------------------------+
&#124;            &#124; taste &#124; land speed &#124; life &#124;
+----------------------------------------+
&#124; data       &#124;       &#124;            &#124;      &#124;
&#124; data       &#124;       &#124;            &#124;      &#124;
&#124; data       &#124;       &#124;            &#124;      &#124;
+----------------------------------------+</description>
		<content:encoded><![CDATA[<p>Another thing (and maybe Python has a module that does this) is to print pretty tables:</p>
<p>+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+<br />
|            | taste | land speed | life |<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+<br />
| data       |       |            |      |<br />
| data       |       |            |      |<br />
| data       |       |            |      |<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: duke</title>
		<link>http://ginstrom.com/scribbles/2007/09/04/pretty-printing-a-table-in-python/comment-page-1/#comment-13</link>
		<dc:creator>duke</dc:creator>
		<pubDate>Tue, 01 Apr 2008 16:30:13 +0000</pubDate>
		<guid isPermaLink="false">http://www.ginstrom.com/scribbles/2007/09/04/pretty-printing-a-table-in-python/#comment-13</guid>
		<description>i used your module and the result looked very nice. Thanks a lot.

Duke</description>
		<content:encoded><![CDATA[<p>i used your module and the result looked very nice. Thanks a lot.</p>
<p>Duke</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Vinicius Ruoso</title>
		<link>http://ginstrom.com/scribbles/2007/09/04/pretty-printing-a-table-in-python/comment-page-1/#comment-11</link>
		<dc:creator>Vinicius Ruoso</dc:creator>
		<pubDate>Fri, 14 Dec 2007 11:55:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.ginstrom.com/scribbles/2007/09/04/pretty-printing-a-table-in-python/#comment-11</guid>
		<description>Yeah, its possible. I just change one caracter to get the expected out.
Thanks again</description>
		<content:encoded><![CDATA[<p>Yeah, its possible. I just change one caracter to get the expected out.<br />
Thanks again</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ryan Ginstrom</title>
		<link>http://ginstrom.com/scribbles/2007/09/04/pretty-printing-a-table-in-python/comment-page-1/#comment-10</link>
		<dc:creator>Ryan Ginstrom</dc:creator>
		<pubDate>Fri, 14 Dec 2007 07:32:30 +0000</pubDate>
		<guid isPermaLink="false">http://www.ginstrom.com/scribbles/2007/09/04/pretty-printing-a-table-in-python/#comment-10</guid>
		<description>Hi Vinicius -- glad to hear you solved your problem. I guess it would make the code more flexible to enable configuration of the justification style.</description>
		<content:encoded><![CDATA[<p>Hi Vinicius &#8212; glad to hear you solved your problem. I guess it would make the code more flexible to enable configuration of the justification style.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

