-
Notifications
You must be signed in to change notification settings - Fork 1
/
SingleTranslate
executable file
·33 lines (27 loc) · 1.13 KB
/
SingleTranslate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env python
from argparse import ArgumentParser, FileType
import sys
def parse_args():
parser = ArgumentParser()
parser.add_argument("--from-col", "-f", default=0, type=int)
parser.add_argument("--to-col", "-t", default=1, type=int)
parser.add_argument("--col-to-translate", "-k", nargs="+", type=int)
parser.add_argument("--delimiter", "-d", default="\t")
parser.add_argument("translation", type=FileType("r"))
parser.add_argument("input", type=FileType("r"))
parser.add_argument("output", type=FileType("w"))
return parser.parse_args()
if __name__ == "__main__":
args = parse_args()
replacements = {
line.split()[args.from_col]: line.split()[args.to_col]
for line in args.translation
}
for line in args.input:
all_cols = line.rstrip("\n").split(args.delimiter)
for col in args.col_to_translate:
for from_str, to_str in replacements.items():
if col < len(all_cols) and all_cols[col] == from_str:
all_cols[col] = to_str
break
print(*all_cols, sep=args.delimiter, file=args.output)