Mercurial > repos > mvdbeek > add_input_name_as_column
diff add_input_name_as_column.py @ 1:07d2cbf43b51 draft
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/add_input_name_as_column commit db90fc6943b99322a6d7459f644a6624c69a0be5-dirty
author | mvdbeek |
---|---|
date | Wed, 06 Mar 2019 09:15:49 -0500 |
parents | bfebefdd5ba4 |
children | ea4ad41e01fc |
line wrap: on
line diff
--- a/add_input_name_as_column.py Mon Dec 29 21:26:29 2014 +0100 +++ b/add_input_name_as_column.py Wed Mar 06 09:15:49 2019 -0500 @@ -1,25 +1,25 @@ -import sys +import io import argparse + def Parser(): - the_parser = argparse.ArgumentParser(description="add label to last column of file") - the_parser.add_argument('--input', required=True, action="store", type=str, help="input tabular file") - the_parser.add_argument('--output', required=True, action="store", type=str, help="output file path") - the_parser.add_argument('--label', required=True, action="store", type=str, help="label to add in last column") - the_parser.add_argument('--header', action="store", type=str, help="column label for last column") - args = the_parser.parse_args() - return args + the_parser = argparse.ArgumentParser(description="add label to last column of file") + the_parser.add_argument('--input', required=True, action="store", type=str, help="input tabular file") + the_parser.add_argument('--output', required=True, action="store", type=str, help="output file path") + the_parser.add_argument('--label', required=True, action="store", type=str, help="label to add in last column") + the_parser.add_argument('--header', action="store", type=str, help="column label for last column") + args = the_parser.parse_args() + return args -args=Parser() -input=open(args.input) -output=open(args.output, 'w') -for i,line in enumerate(input): - line=line.strip('\n') - if (i==0) and (args.header!=None): - line=line+'\t'+args.header - else: - line=line+'\t'+args.label - print >>output, line -input.close() -output.close() +args = Parser() + + +with io.open(args.input, encoding="utf-8") as input, io.open(args.output, 'w', encoding="utf-8") as output: + for i, line in enumerate(input): + line = line.strip('\n') + if (i == 0) and args.header: + line = "%s\t%s\n" % (line, args.header) + else: + line = "%s\t%s\n" % (line, args.label) + output.write(line)