-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpivot
executable file
·59 lines (46 loc) · 1.44 KB
/
pivot
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
#!/usr/bin/python
# Deprecated - Use q instead - https://github.com/harelba/q
import os,sys
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-F","--delimiter",dest="delimiter",default=None,
help="delimiter")
parser.add_option("-f","--key-field-number",dest="key_field_number",default=1,
help="key field")
parser.add_option("-v","--value-field-number",dest="value_field_number",default=2,
help="value field")
parser.add_option("-d","--delimiting-key",dest="delimiting_key",
help="delimiting key")
(options,args) = parser.parse_args()
options.key_field_number = int(options.key_field_number)
options.value_field_number = int(options.value_field_number)
if len(args) != 0:
parser.print_usage()
sys.exit(1)
m_keys = []
m = {}
def collect(key,value):
m_keys.append(key)
m[key] = value
def parse_line(line):
if not options.delimiter:
s = line.split()
else:
s = line.split(options.delimiter)
key_field = s[options.key_field_number-1]
value_field = s[options.value_field_number-1]
eol = key_field == options.delimiting_key
return key_field,value_field,eol
row_count = 0
line = sys.stdin.readline()
while line:
k,v,eol = parse_line(line)
collect(k,v)
if eol:
if row_count == 0:
print "\t".join(["%-20s" % x for x in m_keys])
print "\t".join(["%-20s" % m[x] for x in m_keys])
m = {}
m_keys= []
row_count += 1
line = sys.stdin.readline()