-
Notifications
You must be signed in to change notification settings - Fork 1
/
add-missing.py
executable file
·58 lines (49 loc) · 1.94 KB
/
add-missing.py
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python3
import sys
import argparse
from signal import signal, SIGPIPE, SIG_DFL
signal(SIGPIPE,SIG_DFL)
def pos_int(val):
ival = int(val)
if ival <= 0:
raise argparse.ArgumentTypeError("requires a positive integer")
return ival
# ----- command line parsing -----
parser = argparse.ArgumentParser(
description="Adds missing values in a sequential column.")
parser.add_argument("input", type=str, default=sys.stdin, nargs="?",
help="Input file.")
parser.add_argument("-n", "--numcols", type=pos_int, default=1,
help="Number of columns in file.")
parser.add_argument("-c", "--column", type=pos_int, default=1,
help="Number of column to make sequential.")
parser.add_argument("-d", "--delimiter", type=str, default=None,
help="Column delimiter.")
parser.add_argument("-v", "--starting_value", type=int, default=1,
help="Starting value of sequential column.")
parser.add_argument("-e", "--empty", type=str, default="-",
help="Replace missing fields with EMPTY.")
# ----- end command line parsing -----
def main():
args = parser.parse_args()
if args.delimiter is None:
out_delimiter = " "
else:
out_delimiter = args.delimiter
expected = args.starting_value
before_cols = out_delimiter.join([args.empty]*(args.column-1) + [""])
after_cols = out_delimiter.join([""] + [args.empty]*(args.numcols-args.column))
for line in args.input:
cols = line.split(args.delimiter)
found = int(cols[args.column-1])
if found > expected:
for x in range(expected, found):
sys.stdout.write(before_cols)
sys.stdout.write(str(x))
sys.stdout.write(after_cols)
sys.stdout.write("\n")
expected = found
sys.stdout.write(line)
expected += 1
if __name__ == '__main__':
main()