-
Notifications
You must be signed in to change notification settings - Fork 3
/
counter.py
executable file
·61 lines (53 loc) · 1.68 KB
/
counter.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
#!/usr/bin/env python
import argparse
def proc(infile):
rows = []
columns = []
curr_column = []
with open(infile) as f:
for line in f:
line = line.strip().decode('utf8', 'ignore')
if line.startswith('%'):
line = line.replace('%', '').strip()
columns.append(list(curr_column))
curr_column = []
if line.startswith('#'):
line = line.replace('#', '').strip()
columns.append(list(curr_column))
rows.append(list(columns))
columns = []
curr_column = []
curr_column.append(line)
columns.append(list(curr_column))
rows.append(list(columns))
# unify rows
unified_columns = []
for row in rows:
for (i, col) in enumerate(row):
try:
unified_columns[i] += col
except IndexError:
unified_columns.append(col)
columns = unified_columns
max_widths = [ max([ len(line) for line in col ]) for col in columns ]
max_rows = max([ len(col) for col in columns ])
s = []
for i in xrange(max_rows):
r = ''
for (j, col) in enumerate(columns):
width = max_widths[j]
try:
val = col[i]
except IndexError:
val = ''
padding = width + 3 - len(val)
r += val
if j < len(columns) - 1:
r += ' ' * padding
s.append(r)
print '\n'.join(s)
print 'MAXLEN:%d' % max([len(r) for r in s])
a = argparse.ArgumentParser()
a.add_argument('infile')
p = a.parse_args()
proc(p.infile)