new option: -c --count: count rows in ascii table and add column "row" with row number

This commit is contained in:
Martin Diehl 2012-02-16 11:16:36 +00:00
parent edd5adafd0
commit 9f45cb3745
1 changed files with 8 additions and 5 deletions

View File

@ -39,15 +39,16 @@ parser.add_option('-l','--label', dest='labels', action='extend', type='string
help='(list of) new column labels') help='(list of) new column labels')
parser.add_option('-f','--formula', dest='formulas', action='extend', type='string', \ parser.add_option('-f','--formula', dest='formulas', action='extend', type='string', \
help='(list of) formulas corresponding to labels') help='(list of) formulas corresponding to labels')
parser.add_option('-c','--count', dest='counter', action='store_true', \
help='adds row counter as column "row"')
parser.set_defaults(labels= []) parser.set_defaults(labels= [])
parser.set_defaults(formulas= []) parser.set_defaults(formulas= [])
parser.set_defaults(counter= False)
(options,filenames) = parser.parse_args() (options,filenames) = parser.parse_args()
if len(options.labels) != len(options.formulas): if len(options.labels) != len(options.formulas):
parser.error('number of labels (%i) and formulas (%i) do not match'%(len(options.labels),len(options.formulas))) parser.error('number of labels (%i) and formulas (%i) do not match'%(len(options.labels),len(options.formulas)))
# ------------------------------------------ setup file handles --------------------------------------- # ------------------------------------------ setup file handles ---------------------------------------
files = [] files = []
@ -85,17 +86,19 @@ for file in files:
formula = formula.replace('#'+operand+'#','%e') formula = formula.replace('#'+operand+'#','%e')
evaluator[label] = "'" + formula + "'%(" + ','.join(interpolator) + ")" evaluator[label] = "'" + formula + "'%(" + ','.join(interpolator) + ")"
if options.counter: table.labels.append('row')
# ------------------------------------------ assemble header --------------------------------------- # ------------------------------------------ assemble header ---------------------------------------
table.head_write() table.head_write()
# ------------------------------------------ process data --------------------------------------- # ------------------------------------------ process data ---------------------------------------
i = 0
while table.data_read(): # read next data line of ASCII table while table.data_read(): # read next data line of ASCII table
for label in options.labels: for label in options.labels:
table.data_append(eval(eval(evaluator[label]))) table.data_append(eval(eval(evaluator[label])))
i = i + 1
if options.counter: table.data_append(i)
table.data_write() # output processed line table.data_write() # output processed line
# ------------------------------------------ output result --------------------------------------- # ------------------------------------------ output result ---------------------------------------