diff options
| author | Bjoern Brandenburg <bbb@bbb1-cs.cs.unc.edu> | 2008-09-05 10:01:57 -0400 |
|---|---|---|
| committer | Bjoern Brandenburg <bbb@bbb1-cs.cs.unc.edu> | 2008-09-05 10:01:57 -0400 |
| commit | 5e3ea2b647cab744481e1dfa19fa1683bcdbac4b (patch) | |
| tree | 68f2fdb91e3d2ef70298f00e6569371c8bc8d57c /csv_tool | |
| parent | 7db0258b5c49be554bface11249fa6c296f3348b (diff) | |
support writing output to individual files
Diffstat (limited to 'csv_tool')
| -rwxr-xr-x | csv_tool | 24 |
1 files changed, 19 insertions, 5 deletions
| @@ -9,6 +9,7 @@ import defapp | |||
| 9 | 9 | ||
| 10 | import csv | 10 | import csv |
| 11 | import operator | 11 | import operator |
| 12 | import os.path | ||
| 12 | from collections import defaultdict as defdict | 13 | from collections import defaultdict as defdict |
| 13 | 14 | ||
| 14 | o = optparse.make_option | 15 | o = optparse.make_option |
| @@ -22,6 +23,9 @@ opts = [ | |||
| 22 | o('-c', '--column', action='store', dest='col', type='int', | 23 | o('-c', '--column', action='store', dest='col', type='int', |
| 23 | help='The column on which to operate.'), | 24 | help='The column on which to operate.'), |
| 24 | 25 | ||
| 26 | o(None, '--write-to-file', action='store_true', dest='write_to_file', | ||
| 27 | help='Write the output of operation xyz on file abc.csv to xyz_abc.csv.'), | ||
| 28 | |||
| 25 | # o(None, '--true', action='store_true', dest='truth', | 29 | # o(None, '--true', action='store_true', dest='truth', |
| 26 | # help='A boolean flag value.'), | 30 | # help='A boolean flag value.'), |
| 27 | 31 | ||
| @@ -30,8 +34,9 @@ opts = [ | |||
| 30 | ] | 34 | ] |
| 31 | 35 | ||
| 32 | defaults = { | 36 | defaults = { |
| 33 | 'col' : 0, | 37 | 'col' : 1, |
| 34 | 'col_xcgh' : [], | 38 | 'col_xcgh' : [], |
| 39 | 'write_to_file' : False, | ||
| 35 | } | 40 | } |
| 36 | 41 | ||
| 37 | def make_vector_op(op): | 42 | def make_vector_op(op): |
| @@ -109,9 +114,11 @@ def select_by_key(rows, col, cast=None): | |||
| 109 | class CsvApp(defapp.App): | 114 | class CsvApp(defapp.App): |
| 110 | def __init__(self): | 115 | def __init__(self): |
| 111 | defapp.App.__init__(self, opts, defaults) | 116 | defapp.App.__init__(self, opts, defaults) |
| 117 | # fixup human-friendly offsets | ||
| 112 | self.options.col -= 1 | 118 | self.options.col -= 1 |
| 113 | self.options.col_xchg = [(x - 1, y - 1) for (x, y) in | 119 | if self.options.col_xchg: |
| 114 | self.options.col_xchg] | 120 | self.options.col_xchg = [(x - 1, y - 1) for (x, y) in |
| 121 | self.options.col_xchg] | ||
| 115 | 122 | ||
| 116 | def transform(self, make_iterator, ordered=True): | 123 | def transform(self, make_iterator, ordered=True): |
| 117 | """Average all rows with the same key in a given column.""" | 124 | """Average all rows with the same key in a given column.""" |
| @@ -129,7 +136,14 @@ class CsvApp(defapp.App): | |||
| 129 | else: | 136 | else: |
| 130 | rows = make_iterator(rows) | 137 | rows = make_iterator(rows) |
| 131 | # write out | 138 | # write out |
| 132 | csv.writer(self.outfile()).writerows(rows) | 139 | outfile = self.outfile() |
| 140 | if self.options.write_to_file: | ||
| 141 | (dir, file) = os.path.split(fn) | ||
| 142 | fn = os.path.join(dir, self.args[0] + '_' + file) | ||
| 143 | outfile = open(fn, 'w') | ||
| 144 | csv.writer(outfile).writerows(rows) | ||
| 145 | if self.options.write_to_file: | ||
| 146 | outfile.close() | ||
| 133 | except IOError, ex: | 147 | except IOError, ex: |
| 134 | self.err("%s:%s" % (fn, str(ex))) | 148 | self.err("%s:%s" % (fn, str(ex))) |
| 135 | except IndexError, ex: | 149 | except IndexError, ex: |
