-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmx-txt-viewtabular
More file actions
executable file
·56 lines (49 loc) · 1.74 KB
/
mx-txt-viewtabular
File metadata and controls
executable file
·56 lines (49 loc) · 1.74 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
DELIMITED (CSV/TSV) FILE VIEWER
James B. Pease
"""
import sys
import argparse
from pylib.mixcore import _LICENSE
def generate_argparser():
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawTextHelpFormatter,
epilog=_LICENSE)
parser.add_argument('datafile')
parser.add_argument('--delim', default=','
help='field delimited, use "TAB" for tabs')
parser.add_argument('--tsv',
action="store_true",
help="for tab-separated value")
parser.add_argument('--nlines', type=int)
parser.add_argument('--length', default=20,
type=int, help="field length")
parser.add_argument('--no-header', action='store_true',
help="skip the header")
return parser
def main(arguments=None):
arguments = arguments if arguments is not None else sys.argv[1:]
parser = generate_argparser()
args = parser.parse_args(args=arguments)
nlines = args.nlines + 0 if args.nlines is not None else None
firstline = True
if args.delim == 'TAB' or args.tsv is True:
args.delim = '\t'
with open(args.datafile, 'rt') as infile:
for line in infile:
arr = line.rstrip().split(args.delim)
print("|".join("{}{}".format(
x, " "*(args.length - len(x)))[:args.length] for x in arr))
if firstline is True:
firstline = False
print("-"*((args.length + 1)*len(arr)))
if nlines is not None:
nlines -= 1
if nlines == 0:
break
return ''
if __name__ == '__main__':
main()