-
Notifications
You must be signed in to change notification settings - Fork 3
/
csv2table.py
executable file
·25 lines (24 loc) · 1.22 KB
/
csv2table.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/usr/bin/env python
__author__ = "Scott Hendrickson"
__email__ = "[email protected]"
# Read a comma delimited file and output formatted text table.
# Assumes header row is first
import csv
import fileinput
from FormatTable import FormatTable
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-l", "--latex", dest="latex", default=False, action="store_true",
help="Generate LaTeX table (uses siunitx).")
parser.add_option("-s", "--sig-figs", dest="sf", default=None,
help="Significant figures (default is 4).")
parser.add_option("-c", "--sig-figs-by-column", dest="sflist", default=None,
help='Significant figures by column as list e.g. "[1,2,2,2,4]"')
parser.add_option("-d", "--delimiter", dest="delim", default=",",
help='Input file delimiter (default is comma)')
parser.add_option("-w", "--max-column-width", dest="maxColWidth", default=50,
help='Maximum column width in characters (default is 50 char)')
(options, args) = parser.parse_args()
if options.sf is None and options.sflist is None:
options.sf = 4
print FormatTable([row for row in csv.reader(fileinput.FileInput(args), delimiter = options.delim)], options.sf, options.latex, options.sflist, options.maxColWidth)