-
Notifications
You must be signed in to change notification settings - Fork 0
/
tsv2md.py
122 lines (97 loc) · 3.48 KB
/
tsv2md.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python
"""
Description
Create markdown table from the beginning of a tsv file
Usage
python tsv2md.py --file name [--line 1 --transpose]
Note
- works with python 2.7 and 3.6
Author
David Laperriere <dlaperriere@outlook.com>
"""
import argparse
import textwrap
__version_info__ = (1, 0)
__version__ = '.'.join(map(str, __version_info__))
__author__ = "David Laperriere dlaperriere@outlook.com"
sep = "\t"
md_sep = " | "
def build_argparser():
""" Build command line arguments parser """
parser = argparse.ArgumentParser(
prog=__file__,
formatter_class=argparse.RawDescriptionHelpFormatter,
description=textwrap.dedent('''
Create markdown table from the beginning of a tsv file
'''),
epilog=textwrap.dedent('''
Example
python tsv2md.py --file name [--line 1 --transpose]
''')
)
parser.add_argument('-f', '--file',
type=argparse.FileType('r'),
required=True,
default=None,
help='tsv file')
parser.add_argument('-l', '--line',
type=int,
required=False,
default=1,
help='line to include in markdown table')
parser.add_argument('-t', '--transpose',
action='count',
required=False,
default=0,
help='print columns as rows')
parser.add_argument('-v', '--version', action='version',
version="%(prog)s v" + __version__)
return parser
def main():
""" Main: parse arguments and print markdown table """
# parse command line arguments
parser = build_argparser()
pyargs = parser.parse_args()
# read tsv file
line_count = 0
for line in pyargs.file:
line = line.strip()
if line_count == 0:
header = line.split(sep)
else:
tsv_line = line.split(sep)
if line_count == pyargs.line:
break
line_count += 1
# print markdown table
header_len = list(map(len, header))
tsv_len = list(map(len, tsv_line))
header_line = list()
if pyargs.transpose > 0:
# transpose columns as rows
col_max_length = max(header_len)
ex_max_length = max(tsv_len)
theader = ["column" + " " * (col_max_length - len("column")),
"example" + " " * (ex_max_length - len("example"))]
theader_line = ["-" * col_max_length, "-" * ex_max_length]
print(md_sep.join(theader))
print(md_sep.join(theader_line))
for col, ex in zip(header, tsv_line):
col = col + " " * (col_max_length - len(col))
ex = ex + " " * (ex_max_length - len(ex))
print(col + md_sep + ex)
else:
# as is
for i in range(0, len(header)):
max_length = max(header_len[i], tsv_len[i])
header[i] = header[i] + " " * (max_length - len(header[i]))
header_line.append("-" * max_length)
tsv_line[i] = tsv_line[i] + " " * (max_length - len(tsv_line[i]))
print(md_sep.join(header))
print(md_sep.join(header_line))
print(md_sep.join(tsv_line))
print("")
print("")
if __name__ == "__main__":
main()
exit(0)