changeset 43:bcf9ec537bda

read from stdin if no files specified
author Jeff Hammel <k0scist@gmail.com>
date Tue, 23 Dec 2014 14:31:30 -0800
parents 035e15e99e57
children 7927d7127f9a
files numerics/read.py
diffstat 1 files changed, 9 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/numerics/read.py	Tue Dec 23 13:54:34 2014 -0800
+++ b/numerics/read.py	Tue Dec 23 14:31:30 2014 -0800
@@ -9,13 +9,14 @@
 import argparse
 import csv
 import os
-import subprocess
 import sys
 
+
 # module globals
 __all__ = ['main', 'CSVParser']
 string = (str, unicode)
 
+
 class CSVSchema(object):
     """read CSV with a schema"""
 
@@ -83,14 +84,15 @@
 
     return retval
 
+
 class CSVParser(argparse.ArgumentParser):
     """CLI option parser"""
 
     def __init__(self, **kwargs):
         kwargs.setdefault('description', __doc__)
         argparse.ArgumentParser.__init__(self, **kwargs)
-        self.add_argument('csv', nargs='+',
-                          help="CSV files to read")
+        self.add_argument('csv', nargs='*',
+                          help="CSV files to read, or read from stdin")
         self.add_argument('-+', '--add', dest='added_columns', nargs='+',
                           help="append this column")
         self.add_argument('-c', '--col', '--columns', dest='columns',
@@ -120,6 +122,10 @@
     parser = CSVParser()
     options = parser.parse_args(args)
 
+    if not options.csv:
+        # read from stdin
+        options.csv = [sys.stdin]
+
     # read CSV
     data = read_csv(*options.csv)