-
Notifications
You must be signed in to change notification settings - Fork 4
/
conv_txt2ls.py
executable file
·34 lines (27 loc) · 1.04 KB
/
conv_txt2ls.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
#!/usr/bin/env python
import sys
import collections
import utils
try:
import argparse as ap
import bz2
except ImportError:
sys.stderr.write( "argparse not found" )
sys.exit(-1)
def read_params( args ):
p = ap.ArgumentParser(description='Convert txt files to libsvm\n')
p.add_argument( 'txt', nargs='?', default=None, type=str,
help= "the input txt file [stdin if not present]")
p.add_argument('ls', nargs='?', default=None, type=str,
help= "the output ilibsvm file compressed if fiven with bz2 extension\n"
"[stdout if not present]")
return vars( p.parse_args() )
if __name__ == "__main__":
args = read_params( sys.argv )
uc2cl = collections.defaultdict( set )
with utils.openr(args['txt']) as inp:
data = zip(*[l.strip().split('\t') for l in inp])
outd = [[d[0]]+[str(i+1)+":"+dd for i,dd in enumerate(d[1:])] for d in data[1:]]
with utils.openw(args['ls']) as out:
for o in outd:
out.write( "\t".join(o) +"\n" )