aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBjoern Brandenburg <bbb@bbb1-cs.cs.unc.edu>2008-09-04 22:36:50 -0400
committerBjoern Brandenburg <bbb@bbb1-cs.cs.unc.edu>2008-09-04 22:36:50 -0400
commit7db0258b5c49be554bface11249fa6c296f3348b (patch)
tree9ee6fa6bf5fa4d5f17d812a02ea03fbe79ec6ae0
parent76ae48f526cfad971ef921601a50460bbf1553a5 (diff)
teach csv_tool to reorder columns
-rwxr-xr-xcsv_tool25
1 files changed, 22 insertions, 3 deletions
diff --git a/csv_tool b/csv_tool
index 10f5ee0..674c51c 100755
--- a/csv_tool
+++ b/csv_tool
@@ -15,8 +15,9 @@ o = optparse.make_option
15 15
16opts = [ 16opts = [
17 17
18# o('-t', '--two', action='store', dest='double_val', nargs=2, type='int', 18 o('-x', '--exchange', action='append', dest='col_xchg',
19# help='A two-parameter option.'), 19 nargs=2, type='int',
20 help='Columns that should be switched with reorder.'),
20 21
21 o('-c', '--column', action='store', dest='col', type='int', 22 o('-c', '--column', action='store', dest='col', type='int',
22 help='The column on which to operate.'), 23 help='The column on which to operate.'),
@@ -30,6 +31,7 @@ opts = [
30 31
31defaults = { 32defaults = {
32 'col' : 0, 33 'col' : 0,
34 'col_xcgh' : [],
33 } 35 }
34 36
35def make_vector_op(op): 37def make_vector_op(op):
@@ -84,6 +86,14 @@ def transpose(rows):
84 for i in xrange(c): 86 for i in xrange(c):
85 yield [at(j, i) for j in xrange(r) ] 87 yield [at(j, i) for j in xrange(r) ]
86 88
89
90def reorder_columns(rows, xchg_pairs):
91 for r in rows:
92 print type(r)
93 for (x,y) in xchg_pairs:
94 r[x], r[y] = r[y], r[x]
95 yield r
96
87def select_by_key(rows, col, cast=None): 97def select_by_key(rows, col, cast=None):
88 by_key = defdict(list) 98 by_key = defdict(list)
89 order = [] 99 order = []
@@ -99,6 +109,9 @@ def select_by_key(rows, col, cast=None):
99class CsvApp(defapp.App): 109class CsvApp(defapp.App):
100 def __init__(self): 110 def __init__(self):
101 defapp.App.__init__(self, opts, defaults) 111 defapp.App.__init__(self, opts, defaults)
112 self.options.col -= 1
113 self.options.col_xchg = [(x - 1, y - 1) for (x, y) in
114 self.options.col_xchg]
102 115
103 def transform(self, make_iterator, ordered=True): 116 def transform(self, make_iterator, ordered=True):
104 """Average all rows with the same key in a given column.""" 117 """Average all rows with the same key in a given column."""
@@ -110,7 +123,8 @@ class CsvApp(defapp.App):
110 rows = csv.reader(open(fn, 'r')) 123 rows = csv.reader(open(fn, 'r'))
111 # set up transformation 124 # set up transformation
112 if ordered: 125 if ordered:
113 (order, by_key) = select_by_key(rows, self.options.col, float) 126 (order, by_key) = select_by_key(rows, self.options.col,
127 float)
114 rows = make_iterator(order, by_key) 128 rows = make_iterator(order, by_key)
115 else: 129 else:
116 rows = make_iterator(rows) 130 rows = make_iterator(rows)
@@ -137,5 +151,10 @@ class CsvApp(defapp.App):
137 def do_transpose(self, _): 151 def do_transpose(self, _):
138 self.transform(transpose, ordered=False) 152 self.transform(transpose, ordered=False)
139 153
154 def do_reorder(self, _):
155 self.transform(lambda rows: reorder_columns(rows,
156 self.options.col_xchg),
157 ordered=False)
158
140if __name__ == '__main__': 159if __name__ == '__main__':
141 CsvApp().launch() 160 CsvApp().launch()