Skip to content

Instantly share code, notes, and snippets.

@jiffyclub
Created October 20, 2012 01:04
Show Gist options
  • Save jiffyclub/3921540 to your computer and use it in GitHub Desktop.
Save jiffyclub/3921540 to your computer and use it in GitHub Desktop.
Print the contents of two files to a two column HTML table with headings 'Good' and 'Bad'. Files need not have the same length.
#!/usr/bin/env python
"""
Print the contents of two files to a two column HTML table
with headings 'Good' and 'Bad'. Files need not have the same length.
"""
import argparse
import itertools
import sys
def print_table(goodfile, badfile):
table = """<table>
<thead>
<tr>
<th>Good</th>
<th>Bad</th>
</tr>
</thead>
<tbody style="font-size: smaller;">
"""
template = '<tr>\n<td>{}</td><td>{}</td>\n</tr>\n'
for g, b in itertools.izip_longest(goodfile, badfile, fillvalue=''):
g = g.strip()
b = b.strip()
table += template.format(g, b)
table += '</tbody>\n</table>\n'
return table
def parse_args(args=None):
parser = argparse.ArgumentParser('Print good/bad feedback HTML table.')
parser.add_argument('goodfile', type=argparse.FileType(),
help='File of good feedback.')
parser.add_argument('badfile', type=argparse.FileType(),
help='File of bad feedback.')
return parser.parse_args(args)
def main(args=None):
args = parse_args(args)
print print_table(args.goodfile, args.badfile)
args.goodfile.close()
args.badfile.close()
if __name__ == '__main__':
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment