-
Notifications
You must be signed in to change notification settings - Fork 0
/
recview.py
executable file
·65 lines (56 loc) · 1.46 KB
/
recview.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
59
60
61
62
63
64
65
#!/usr/bin/env python3
import sys
import csv
import argparse
parser = argparse.ArgumentParser(description='''This script displays the
contents of a tsv or csv file, row-by-row with column headers for each
field''')
parser.add_argument('-i', '--input', help='input file, default is stdin')
parser.add_argument('-d', '--delimiter', help='character used for delimiter;'
'default is tab')
parser.add_argument('-z', '--zero_based', help='start numbering from zero',
action='store_true')
parser.add_argument('-nh', '--noheader', help='no header present',
action='store_true')
args = parser.parse_args()
infile = args.input
# See http://stackoverflow.com/questions/14207708/ioerror-errno-32-broken-pipe-python
from signal import signal, SIGPIPE, SIG_DFL
signal(SIGPIPE, SIG_DFL)
def recview():
if not args.noheader:
header = next(tbl)
l = 0
for item in header:
if l < len(item):
l = len(item)
for row in tbl:
i = z
j = 0
while (j < len(row) ):
print("{:>{}} [{:3d}]: {}" .format(header[j], l, i, row[j]))
i, j = i+1, j+1
print('\n')
else:
for row in tbl:
i = z
j = 0
while (j < len(row) ):
print("[{:3d}] {}" .format(i, row[j]))
i, j = i+1, j+1
print('\n')
if args.delimiter:
delim = args.delimiter
else:
delim = '\t'
if args.zero_based:
z = 0
else:
z = 1
if args.input:
with open(infile, 'r') as f:
tbl = csv.reader(f, delimiter = delim)
recview()
else:
tbl = csv.reader(sys.stdin, delimiter = delim)
recview()