Mercurial > hg > numerics
changeset 28:2cadb4349753
parse by columns
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Wed, 08 Oct 2014 11:12:28 -0700 |
parents | f865bc916593 |
children | c2c92c8da611 |
files | numerics/read.py |
diffstat | 1 files changed, 8 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/numerics/read.py Tue Oct 07 21:04:36 2014 -0700 +++ b/numerics/read.py Wed Oct 08 11:12:28 2014 -0700 @@ -85,6 +85,7 @@ class CSVParser(argparse.ArgumentParser): """CLI option parser""" + def __init__(self, **kwargs): kwargs.setdefault('description', __doc__) argparse.ArgumentParser.__init__(self, **kwargs) @@ -92,6 +93,9 @@ help="CSV files to read") self.add_argument('-+', '--add', dest='added_columns', nargs='+', help="append this column") + self.add_argument('-c', '--col', '--columns', dest='columns', + nargs='+', type=int, + help="column numbers to output, starting with 0") self.add_argument('-o', '--output', dest='output', type=argparse.FileType('a'), default=sys.stdout, help='output destination, or stdout') @@ -121,6 +125,10 @@ for row in data: row.extend(options.added_columns) + if options.columns: + rows = [[row[column] for column in options[columns]] + for row in rows] + # write CSV writer = csv.writer(options.output) for row in data: