Mercurial > hg > numerics
changeset 11:5609225fb254
read csv
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Thu, 11 Sep 2014 12:46:13 -0700 |
parents | 7517682843cb |
children | a929d14c0701 |
files | numerics/read.py setup.py |
diffstat | 2 files changed, 23 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
--- a/numerics/read.py Thu Sep 04 21:01:36 2014 -0700 +++ b/numerics/read.py Thu Sep 11 12:46:13 2014 -0700 @@ -24,13 +24,27 @@ os.makedirs(directory) return directory -def read_csv +def read_csv(*fp): + retval = [] + for f in fp: + + if isinstance(f, string): + with open(f) as _f: + retval.extend(read_csv(_f)) + continue + + reader = csv.reader(f) + retval.extend([row for row in reader]) + + 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.options = None def parse_args(self, *args, **kw): @@ -46,9 +60,14 @@ """CLI""" # parse command line options - parser = Parser() + parser = CVSParser() options = parser.parse_args(args) + # read CSV + data = read_csv(*options.csv) + from pprint import pprint + pprint(data) + if __name__ == '__main__': main()
--- a/setup.py Thu Sep 04 21:01:36 2014 -0700 +++ b/setup.py Thu Sep 11 12:46:13 2014 -0700 @@ -15,7 +15,8 @@ try: from setuptools import setup kw['entry_points'] = """ - [console_scripts] + [console_scripts] + read-csv = numerics.read:main """ kw['install_requires'] = dependencies except ImportError: