From 5e3ea2b647cab744481e1dfa19fa1683bcdbac4b Mon Sep 17 00:00:00 2001 From: Bjoern Brandenburg Date: Fri, 5 Sep 2008 10:01:57 -0400 Subject: support writing output to individual files --- csv_tool | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/csv_tool b/csv_tool index 674c51c..e747462 100755 --- a/csv_tool +++ b/csv_tool @@ -9,6 +9,7 @@ import defapp import csv import operator +import os.path from collections import defaultdict as defdict o = optparse.make_option @@ -22,6 +23,9 @@ opts = [ o('-c', '--column', action='store', dest='col', type='int', help='The column on which to operate.'), + o(None, '--write-to-file', action='store_true', dest='write_to_file', + help='Write the output of operation xyz on file abc.csv to xyz_abc.csv.'), + # o(None, '--true', action='store_true', dest='truth', # help='A boolean flag value.'), @@ -30,8 +34,9 @@ opts = [ ] defaults = { - 'col' : 0, - 'col_xcgh' : [], + 'col' : 1, + 'col_xcgh' : [], + 'write_to_file' : False, } def make_vector_op(op): @@ -109,9 +114,11 @@ def select_by_key(rows, col, cast=None): class CsvApp(defapp.App): def __init__(self): defapp.App.__init__(self, opts, defaults) + # fixup human-friendly offsets self.options.col -= 1 - self.options.col_xchg = [(x - 1, y - 1) for (x, y) in - self.options.col_xchg] + if self.options.col_xchg: + self.options.col_xchg = [(x - 1, y - 1) for (x, y) in + self.options.col_xchg] def transform(self, make_iterator, ordered=True): """Average all rows with the same key in a given column.""" @@ -129,7 +136,14 @@ class CsvApp(defapp.App): else: rows = make_iterator(rows) # write out - csv.writer(self.outfile()).writerows(rows) + outfile = self.outfile() + if self.options.write_to_file: + (dir, file) = os.path.split(fn) + fn = os.path.join(dir, self.args[0] + '_' + file) + outfile = open(fn, 'w') + csv.writer(outfile).writerows(rows) + if self.options.write_to_file: + outfile.close() except IOError, ex: self.err("%s:%s" % (fn, str(ex))) except IndexError, ex: -- cgit v1.2.2