Last active
December 11, 2015 05:49
-
-
Save informationsea/4555277 to your computer and use it in GitHub Desktop.
Make tex table from Tab separated text or CSV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
__author__ = "Y.Okamura <okamura=AT=informationsea.info>" | |
__copyright__ = "Copyright (C) 2013 Y.Okamura" | |
__license__ = "GPL3 or later" | |
import argparse | |
import csv | |
import sys | |
import itertools | |
def is_float(s): | |
try: | |
float(s) | |
return True | |
except ValueError as e: | |
return False | |
def _main(): | |
parser = argparse.ArgumentParser(description="Make tex table from Tab separated or CSV") | |
parser.add_argument('input', type=argparse.FileType('r'), nargs='?', default=sys.stdin, | |
help='input file [default:stdin]') | |
parser.add_argument('output', type=argparse.FileType('r'), nargs='?', default=sys.stdout, | |
help='output file [default:stdout]') | |
parser.add_argument('-c', '--caption', default='tex table', help='The caption of table [default:%(default)s]') | |
parser.add_argument('-t', '--tab', action='store_true', help='Read tab separated table') | |
parser.add_argument('-l', '--longtable', action='store_true', help='use longtable environment') | |
parser.add_argument('-e', '--header', type=int, default=1, help='Number of head lines [default:%(default)s]') | |
parser.add_argument('-d', '--delimiter', default=',', help='delimiter for input [default:%(default)s]') | |
parser.add_argument('-q', '--quote', default='"', help='quote char for input [default:%(default)s]') | |
parser.add_argument('-r', '--round-real', action='store_true', help='Round real values') | |
parser.add_argument('--sig-fig', default=3, help='Significant figures [default:%(default)s]') | |
options = parser.parse_args() | |
delimiter = options.delimiter | |
quotechar = options.quote | |
if options.tab: | |
delimiter = '\t' | |
quotechar = None | |
reader = csv.reader(options.input, delimiter=delimiter, quotechar=quotechar) | |
first_row = reader.next() | |
columns = '|'+('l|'*len(first_row)) | |
if options.longtable: | |
print >>options.output, '% \\usepackage{longtable} % required for longtable environment' | |
print >>options.output, '''\\begin{center} | |
\\begin{longtable}{'''+columns+'''} | |
\\caption{'''+options.caption+'''} | |
\\\\ | |
\\hline''' | |
else: | |
print >>options.output, '''\\begin{table}[htbp] | |
\\caption{'''+options.caption+'''} | |
\\begin{center} | |
\\begin{tabular}{'''+columns+'''} | |
\\hline''' | |
for i, row in enumerate(itertools.chain([first_row], reader)): | |
if options.round_real: | |
newrow = [('{:.'+str(options.sig_fig)+'e}').format(float(x)) if is_float(x) else x for x in row] | |
row = newrow | |
if options.header == i: | |
print >>options.output, '\\hline' | |
if options.longtable: | |
print >>options.output, '''\\endhead | |
\\hline | |
\\endfoot''' | |
print >>options.output, ' & '.join([x.replace('&', '\\&').replace('_', '\\_') for x in row])+' \\\\' | |
if options.longtable: | |
print >>options.output, '''\\end{longtable} | |
\\end{center}''' | |
else: | |
print >>options.output, '''\\hline | |
\\end{tabular} | |
\\end{center} | |
\\end{table}''' | |
if __name__ == '__main__': | |
_main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment